Changeset View
Changeset View
Standalone View
Standalone View
src/applications/paste/lipsum/PhabricatorPasteTestDataGenerator.php
<?php | <?php | ||||
final class PhabricatorPasteTestDataGenerator | final class PhabricatorPasteTestDataGenerator | ||||
extends PhabricatorTestDataGenerator { | extends PhabricatorTestDataGenerator { | ||||
public function getGeneratorName() { | public function getGeneratorName() { | ||||
return pht('Pastes'); | return pht('Pastes'); | ||||
} | } | ||||
// Better Support for this in the future | |||||
public $supportedLanguages = array( | |||||
vrana: This is still used in `PhabricatorDifferentialRevisionTestDataGenerator`. | |||||
vranaUnsubmitted Not Done Inline ActionsgenerateContent too. vrana: `generateContent` too. | |||||
'Java' => 'java', | |||||
'PHP' => 'php', | |||||
); | |||||
public function generateObject() { | public function generateObject() { | ||||
$author = $this->loadPhabrictorUser(); | $author = $this->loadRandomUser(); | ||||
$authorphid = $author->getPHID(); | |||||
$language = $this->generateLanguage(); | list($name, $language, $content) = $this->newPasteContent(); | ||||
$content = $this->generateContent($language); | |||||
$title = $this->generateTitle($language); | $paste = PhabricatorPaste::initializeNewPaste($author); | ||||
$paste_file = PhabricatorFile::newFromFileData( | |||||
$content, | $xactions = array(); | ||||
array( | |||||
'name' => $title, | $xactions[] = $this->newTransaction( | ||||
'mime-type' => 'text/plain; charset=utf-8', | PhabricatorPasteTransaction::TYPE_TITLE, | ||||
'authorPHID' => $authorphid, | $name); | ||||
)); | |||||
$policy = $this->generatePolicy(); | $xactions[] = $this->newTransaction( | ||||
$filephid = $paste_file->getPHID(); | PhabricatorPasteTransaction::TYPE_LANGUAGE, | ||||
$parentphid = $this->loadPhabrictorPastePHID(); | $language); | ||||
$paste = PhabricatorPaste::initializeNewPaste($author) | |||||
->setParentPHID($parentphid) | $xactions[] = $this->newTransaction( | ||||
->setTitle($title) | PhabricatorPasteTransaction::TYPE_CONTENT, | ||||
->setLanguage($language) | $content); | ||||
->setViewPolicy($policy) | |||||
->setEditPolicy($policy) | $editor = id(new PhabricatorPasteEditor()) | ||||
->setFilePHID($filephid) | ->setActor($author) | ||||
->save(); | ->setContentSource($this->getLipsumContentSource()) | ||||
->setContinueOnNoEffect(true) | |||||
->applyTransactions($paste, $xactions); | |||||
return $paste; | return $paste; | ||||
} | } | ||||
private function loadPhabrictorPastePHID() { | protected function newEmptyTransaction() { | ||||
$random = rand(0, 1); | return new PhabricatorPasteTransaction(); | ||||
if ($random == 1) { | |||||
$paste = id($this->loadOneRandom('PhabricatorPaste')); | |||||
if ($paste) { | |||||
return $paste->getPHID(); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
public function generateTitle($language = null) { | |||||
$taskgen = new PhutilLipsumContextFreeGrammar(); | |||||
// Remove Punctuation | |||||
$title = preg_replace('/[^a-zA-Z 0-9]+/', '', $taskgen->generate()); | |||||
// Capitalize First Letters | |||||
$title = ucwords($title); | |||||
// Remove Spaces | |||||
$title = preg_replace('/\s+/', '', $title); | |||||
if ($language == null || | |||||
!in_array($language, array_keys($this->supportedLanguages))) { | |||||
return $title.'.txt'; | |||||
} else { | |||||
return $title.'.'.$this->supportedLanguages[$language]; | |||||
} | } | ||||
private function newPasteContent() { | |||||
$languages = array( | |||||
'txt' => array(), | |||||
'php' => array( | |||||
'content' => 'PhutilPHPCodeSnippetContextFreeGrammar', | |||||
), | |||||
'java' => array( | |||||
'content' => 'PhutilJavaCodeSnippetContextFreeGrammar', | |||||
), | |||||
); | |||||
$language = array_rand($languages); | |||||
$spec = $languages[$language]; | |||||
$title_generator = idx($spec, 'title'); | |||||
if (!$title_generator) { | |||||
$title_generator = 'PhabricatorPasteFilenameContextFreeGrammar'; | |||||
} | } | ||||
public function generateLanguage() { | $content_generator = idx($spec, 'content'); | ||||
$supplemented_lang = $this->supportedLanguages; | if (!$content_generator) { | ||||
$supplemented_lang['lipsum'] = 'txt'; | $content_generator = 'PhutilLipsumContextFreeGrammar'; | ||||
return array_rand($supplemented_lang); | |||||
} | |||||
public function generateContent($language = null) { | |||||
if ($language == null || | |||||
!in_array($language, array_keys($this->supportedLanguages))) { | |||||
return id(new PhutilLipsumContextFreeGrammar()) | |||||
->generateSeveral(rand(30, 40)); | |||||
} else { | |||||
$cfg_class = 'Phutil'.$language.'CodeSnippetContextFreeGrammar'; | |||||
return newv($cfg_class, array())->generate(); | |||||
} | } | ||||
$title = newv($title_generator, array()) | |||||
->generate(); | |||||
$content = newv($content_generator, array()) | |||||
->generateSeveral($this->roll(4, 12, 10)); | |||||
// Usually add the language as a suffix. | |||||
if ($this->roll(1, 20) > 2) { | |||||
$title = $title.'.'.$language; | |||||
} | } | ||||
public function generatePolicy() { | switch ($this->roll(1, 20)) { | ||||
// Make sure 4/5th of all generated Pastes are viewable to all | |||||
switch (rand(0, 4)) { | |||||
case 0: | |||||
return PhabricatorPolicies::POLICY_PUBLIC; | |||||
case 1: | case 1: | ||||
return PhabricatorPolicies::POLICY_NOONE; | // On critical miss, set a different, random language. | ||||
$highlight_as = array_rand($languages); | |||||
break; | |||||
case 18: | |||||
case 19: | |||||
case 20: | |||||
// Sometimes set it to the correct language. | |||||
$highlight_as = $language; | |||||
break; | |||||
default: | default: | ||||
return PhabricatorPolicies::POLICY_USER; | // Usually leave it as autodetect. | ||||
$highlight_as = ''; | |||||
break; | |||||
} | } | ||||
return array($title, $highlight_as, $content); | |||||
} | } | ||||
} | } |
This is still used in PhabricatorDifferentialRevisionTestDataGenerator.