public function testAdd()
 {
     $storage = new TranslationStashStorage(wfGetDB(DB_MASTER));
     $translation1 = new StashedTranslation(User::newFromId(1), Title::makeTitle(NS_MAIN, __METHOD__), 'test value', array('kissa', 'kala'));
     $translation2 = new StashedTranslation(User::newFromId(2), Title::makeTitle(NS_MAIN, __METHOD__), 'test value 2', array('kissa', 'kala'));
     $storage->addTranslation($translation1);
     $storage->addTranslation($translation2);
     $ret = $storage->getTranslations(User::newFromId(1));
     $this->assertCount(1, $ret, 'One stashed translation for this user');
     // AssertSame required same reference, assert equals only same content
     $this->assertEquals($translation1, $ret[0], 'Data roundtrips');
 }
 public function execute()
 {
     $users = 10;
     // For number of translations, limited to [0,20]
     $mean = 15;
     $stddev = 20;
     $stash = new TranslationStashStorage(wfGetDB(DB_MASTER));
     $languages = array_keys(Language::fetchLanguageNames());
     for ($i = 0; $i < $users; $i++) {
         $username = '******' . wfRandomString(6);
         $password = wfRandomString(12);
         $email = "{$username}.{$password}@blackhole.io";
         $user = TranslateSandbox::addUser($username, $password, $email);
         $language = $languages[rand(0, count($languages) - 1)];
         $count = wfGaussMs($mean, $stddev);
         $count = min(20, $count);
         $count = max(0, $count);
         for ($j = 0; $j < $count; $j++) {
             $title = Title::makeTitle(NS_MEDIAWIKI, wfRandomString(24) . '/' . $language);
             $value = array('Pupu söi');
             for ($k = rand(0, 20); $k > 0; $k--) {
                 $value[] = wfRandomString(rand(1, 28));
             }
             $value = implode("\n", $value);
             $translation = new StashedTranslation($user, $title, $value);
             $stash->addTranslation($translation);
         }
     }
 }
 /**
  * Add users to the sandbox or delete them to facilitate browsers tests.
  * Use with caution!
  */
 public function prepareForTests()
 {
     global $wgTranslateTestUsers;
     $user = $this->getUser();
     $request = $this->getRequest();
     if (!in_array($user->getName(), $wgTranslateTestUsers, true)) {
         return;
     }
     if ($request->getVal('integrationtesting') === 'populate') {
         // Empty all the users, even if they were created manually
         // to ensure the number of users is what the tests expect
         $this->emptySandbox();
         $textUsernamePrefixes = array('Pupu', 'Orava');
         $testLanguages = array('fi', 'uk', 'nl', 'he', 'bn');
         $testLanguagesCount = count($testLanguages);
         foreach ($textUsernamePrefixes as $prefix) {
             for ($i = 0; $i < $testLanguagesCount; $i++) {
                 $name = "{$prefix}{$i}";
                 // Get rid of users, even if promoted during tests
                 $userToDelete = User::newFromName($name, false);
                 $this->deleteUserPage($userToDelete);
                 TranslateSandbox::deleteUser($userToDelete, 'force');
                 $user = TranslateSandbox::addUser($name, "{$name}@blackhole.io", 'porkkana');
                 $user->setOption('translate-sandbox', FormatJson::encode(array('languages' => array($testLanguages[$i]), 'comment' => '')));
                 $reminders = array();
                 for ($reminderIndex = 0; $reminderIndex < $i; $reminderIndex++) {
                     $reminders[] = wfTimestamp() - $reminderIndex * $i * 10000;
                 }
                 $user->setOption('translate-sandbox-reminders', implode('|', $reminders));
                 $user->saveSettings();
                 for ($j = 0; $j < $i; $j++) {
                     $title = Title::makeTitle(NS_MEDIAWIKI, wfRandomString(24) . '/' . $testLanguages[$i]);
                     $translation = 'plop';
                     $stashedTranslation = new StashedTranslation($user, $title, $translation);
                     $this->stash->addTranslation($stashedTranslation);
                 }
             }
         }
         // Another account for testing a translator to multiple languages
         $oldPolyglotUser = User::newFromName('Kissa', false);
         $this->deleteUserPage($oldPolyglotUser);
         TranslateSandbox::deleteUser($oldPolyglotUser, 'force');
         $polyglotUser = TranslateSandbox::addUser('Kissa', '*****@*****.**', 'porkkana');
         $polyglotUser->setOption('translate-sandbox', FormatJson::encode(array('languages' => $testLanguages, 'comment' => "I know some languages, and I'm a developer.")));
         $polyglotUser->saveSettings();
         for ($polyglotLang = 0; $polyglotLang < $testLanguagesCount; $polyglotLang++) {
             $title = Title::makeTitle(NS_MEDIAWIKI, wfRandomString(24) . '/' . $testLanguages[$polyglotLang]);
             $translation = "plop in {$testLanguages[$polyglotLang]}";
             $stashedTranslation = new StashedTranslation($polyglotUser, $title, $translation);
             $this->stash->addTranslation($stashedTranslation);
         }
     } elseif ($request->getVal('integrationtesting') === 'empty') {
         $this->emptySandbox();
     }
 }
 public function execute()
 {
     $params = $this->extractRequestParams();
     // The user we are operating on, not necessarly the user making the request
     $user = $this->getUser();
     if (isset($params['username'])) {
         if ($this->getUser()->isAllowed('translate-sandboxmanage')) {
             $user = User::newFromName($params['username']);
             if (!$user) {
                 $this->dieUsageMsg(array('invalidparam', 'username'));
             }
         } else {
             $this->dieUsageMsg(array('invalidparam', 'username'));
         }
     }
     $stash = new TranslationStashStorage(wfGetDB(DB_MASTER));
     $action = $params['subaction'];
     if ($action === 'add') {
         if (!isset($params['title'])) {
             $this->dieUsageMsg(array('missingparam', 'title'));
         }
         if (!isset($params['translation'])) {
             $this->dieUsageMsg(array('missingparam', 'translation'));
         }
         // @todo: Return value of Title::newFromText not checked
         $translation = new StashedTranslation($user, Title::newFromText($params['title']), $params['translation'], FormatJson::decode($params['metadata'], true));
         $stash->addTranslation($translation);
     }
     if ($action === 'query') {
         $output['translations'] = array();
         $translations = $stash->getTranslations($user);
         foreach ($translations as $translation) {
             $output['translations'][] = $this->formatTranslation($translation);
         }
     }
     // If we got this far, nothing has failed
     $output['result'] = 'ok';
     $this->getResult()->addValue(null, $this->getModuleName(), $output);
 }