|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorPholioMockTestDataGenerator |
| 4 | + extends PhabricatorTestDataGenerator { |
| 5 | + |
| 6 | + public function generate() { |
| 7 | + $authorPHID = $this->loadPhabrictorUserPHID(); |
| 8 | + $author = id(new PhabricatorUser()) |
| 9 | + ->loadOneWhere('phid = %s', $authorPHID); |
| 10 | + $mock = id(new PholioMock()) |
| 11 | + ->setAuthorPHID($authorPHID); |
| 12 | + $content_source = PhabricatorContentSource::newForSource( |
| 13 | + PhabricatorContentSource::SOURCE_UNKNOWN, |
| 14 | + array()); |
| 15 | + $template = id(new PholioTransaction()) |
| 16 | + ->setContentSource($content_source); |
| 17 | + |
| 18 | + // Accumulate Transactions |
| 19 | + $changes = array(); |
| 20 | + $changes[PholioTransactionType::TYPE_NAME] = |
| 21 | + $this->generateTitle(); |
| 22 | + $changes[PholioTransactionType::TYPE_DESCRIPTION] = |
| 23 | + $this->generateDescription(); |
| 24 | + $changes[PhabricatorTransactions::TYPE_VIEW_POLICY] = |
| 25 | + PhabricatorPolicies::POLICY_PUBLIC; |
| 26 | + $changes[PhabricatorTransactions::TYPE_SUBSCRIBERS] = |
| 27 | + array('=' => $this->getCCPHIDs()); |
| 28 | + |
| 29 | + // Get Files and make Images |
| 30 | + $filePHIDS = $this->generateImages(); |
| 31 | + $files = id(new PhabricatorFileQuery()) |
| 32 | + ->setViewer($author) |
| 33 | + ->withPHIDs($filePHIDS) |
| 34 | + ->execute(); |
| 35 | + $mock->setCoverPHID(head($files)->getPHID()); |
| 36 | + $sequence = 0; |
| 37 | + $images = array(); |
| 38 | + foreach ($files as $file) { |
| 39 | + $image = new PholioImage(); |
| 40 | + $image->setFilePHID($file->getPHID()); |
| 41 | + $image->setSequence($sequence++); |
| 42 | + $images[] = $image; |
| 43 | + } |
| 44 | + |
| 45 | + // Apply Transactions |
| 46 | + $transactions = array(); |
| 47 | + foreach ($changes as $type => $value) { |
| 48 | + $transaction = clone $template; |
| 49 | + $transaction->setTransactionType($type); |
| 50 | + $transaction->setNewValue($value); |
| 51 | + $transactions[] = $transaction; |
| 52 | + } |
| 53 | + $mock->openTransaction(); |
| 54 | + $editor = id(new PholioMockEditor()) |
| 55 | + ->setContentSource($content_source) |
| 56 | + ->setContinueOnNoEffect(true) |
| 57 | + ->setActor($author) |
| 58 | + ->applyTransactions($mock, $transactions); |
| 59 | + foreach ($images as $image) { |
| 60 | + $image->setMockID($mock->getID()); |
| 61 | + $image->save(); |
| 62 | + } |
| 63 | + |
| 64 | + $mock->saveTransaction(); |
| 65 | + return $mock->save(); |
| 66 | + } |
| 67 | + |
| 68 | + private function loadPhabrictorUserPHID() { |
| 69 | + return $this->loadOneRandom("PhabricatorUser")->getPHID(); |
| 70 | + } |
| 71 | + |
| 72 | + public function generateTitle() { |
| 73 | + return id(new PhutilLipsumContextFreeGrammar()) |
| 74 | + ->generate(); |
| 75 | + } |
| 76 | + |
| 77 | + public function generateDescription() { |
| 78 | + return id(new PhutilLipsumContextFreeGrammar()) |
| 79 | + ->generateSeveral(rand(30, 40)); |
| 80 | + } |
| 81 | + |
| 82 | + public function getCCPHIDs() { |
| 83 | + $ccs = array(); |
| 84 | + for ($i = 0; $i < rand(1, 4);$i++) { |
| 85 | + $ccs[] = $this->loadPhabrictorUserPHID(); |
| 86 | + } |
| 87 | + return $ccs; |
| 88 | + } |
| 89 | + |
| 90 | + public function generateImages() { |
| 91 | + $images = newv("PhabricatorFile", array()) |
| 92 | + ->loadAllWhere("mimeType = %s", "image/jpeg"); |
| 93 | + $rand_images = array(); |
| 94 | + $quantity = rand(1, 10); |
| 95 | + foreach (array_rand($images, $quantity) as $random) { |
| 96 | + $rand_images[] = $images[$random]->getPHID(); |
| 97 | + } |
| 98 | + return $rand_images; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +} |
0 commit comments