public function testUpdateThenRemove_NewProject_CreatesThenRemovesProjectDatabase() { $e = new MongoTestEnvironment(); $e->clean(); $project = $e->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); $databaseName = $project->databaseName(); $project->remove(); $db = MongoStore::connect($databaseName); $this->assertEqual(count($db->listCollections()), 0); $text = new TextModel($project); $text->title = 'Some Title'; $text->write(); $this->assertTrue(MongoStore::hasDB($databaseName)); $this->assertEqual(count($db->listCollections()), 1); $project->remove(); $this->assertEqual(count($db->listCollections()), 0); }
/** * Create or update project * @param string $projectId * @param string $userId * @param array<projectModel> $object * @throws UserUnauthorizedException * @throws \Exception * @return string projectId */ public static function updateProject($projectId, $userId, $object) { $project = new LexiconProjectModel($projectId); if (!$project->hasRight($userId, Domain::USERS + Operation::EDIT)) { throw new UserUnauthorizedException("Insufficient privileges to update project in method 'updateProject'"); } $oldDBName = $project->databaseName(); $object['id'] = $projectId; JsonDecoder::decode($project, $object); $newDBName = $project->databaseName(); if ($oldDBName != '' && $oldDBName != $newDBName) { if (MongoStore::hasDB($newDBName)) { throw new \Exception("Cannot rename '{$oldDBName}' to ' {$newDBName}' . New project name {$newDBName} already exists. Not renaming."); } MongoStore::renameDB($oldDBName, $newDBName); } $projectId = $project->write(); return $projectId; }
public function checkProject($projectId) { $project = new ProjectModel($projectId); $this->projectsChecked++; $this->info("Checking {$project->projectName}"); if ($project->projectName == '') { $this->warn("{$projectId} has an empty projectName"); } if ($project->projectCode == '') { $this->warn("{$project->projectName} has an empty projectCode. This will certainly cause failures"); } // check that a database exists for this project try { $databaseName = $project->databaseName(); } catch (\Exception $e) { $databaseName = ""; } if (!MongoStore::hasDB($databaseName)) { $newProjectCode = str_replace(' ', '_', strtolower($project->projectName)); $newDatabaseName = 'sf_' . $newProjectCode; if (MongoStore::hasDB($newDatabaseName)) { $this->warn("projectCode does not correspond to an existing MongoDb but projectName does (db migration required)"); $this->fix("Changed projectCode to {$newProjectCode}"); $this->projectsFixed++; $project->projectCode = $newProjectCode; } else { $this->warn("{$project->projectName} has no corresponding database. (could indicate a brand new project with no data"); } } if ($project->siteName == '') { $this->warn("{$project->projectName} has no corresponding website (will not appear on any site)"); } if ($project->appName == '') { $this->warn("{$project->projectName} has no app associated with it"); } if ($this->makeChanges) { $project->write(); } }
$projectList->read(); print "{$projectList->count} projects will be deleted\n\n"; foreach ($projectList->entries as $p) { $project = new ProjectModel($p['id']); print "Deleting Project " . $project->projectName . "\n"; if ($runForReal) { try { $project->remove(); } catch (\Exception $e) { // don't do anything } } } // start with a fresh database print "\nDropping main database...\n"; if ($runForReal) { $db = \Api\Model\Mapper\MongoStore::connect(SF_DATABASE); foreach ($db->listCollections() as $collection) { $collection->drop(); } } print "\nDropping other dbs on the server (like test dbs)\n"; if ($runForReal) { $cmd = "mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){ if (i.indexOf(\"sf_\") == 0 || i.indexOf(\"scriptureforge\") == 0) { print(\"Dropping \" + i); db.getSiblingDB(i).dropDatabase()}})'"; system($cmd); } print "\nCreating user: admin password: password\n"; if ($runForReal) { $adminUser = UserCommands::createUser(array('id' => '', 'name' => 'Admin', 'email' => '*****@*****.**', 'username' => 'admin', 'password' => 'password', 'active' => true, 'role' => SystemRoles::SYSTEM_ADMIN), $languageforgeWebsite); } print "\n\n";
protected function cleanProjectEnvironment($projectModel) { // clean out old db if it is present $projectDb = \Api\Model\Mapper\MongoStore::connect($projectModel->databaseName()); foreach ($projectDb->listCollections() as $collection) { $collection->drop(); } // clean up assets folder $folderPath = $projectModel->getAssetsFolderPath(); $cleanupFiles = glob($folderPath . '/*'); foreach ($cleanupFiles as $cleanupFile) { @unlink($cleanupFile); } @rmdir($folderPath); }
} } // drop the third database because it is used in a rename test $projectModel = new ProjectModel(); $projectModel->projectName = $constants['thirdProjectName']; $projectModel->projectCode = $constants['thirdProjectCode']; $db = \Api\Model\Mapper\MongoStore::dropDB($projectModel->databaseName()); // drop the 'new' and 'empty' database because it is used in a 'create new project' test $projectModel = new ProjectModel(); $projectModel->projectName = $constants['newProjectName']; $projectModel->projectCode = $constants['newProjectCode']; $db = \Api\Model\Mapper\MongoStore::dropDB($projectModel->databaseName()); $projectModel = new ProjectModel(); $projectModel->projectName = $constants['emptyProjectName']; $projectModel->projectCode = $constants['emptyProjectCode']; $db = \Api\Model\Mapper\MongoStore::dropDB($projectModel->databaseName()); $adminUserId = UserCommands::createUser(array('id' => '', 'name' => $constants['adminName'], 'email' => $constants['adminEmail'], 'username' => $constants['adminUsername'], 'password' => $constants['adminPassword'], 'active' => true, 'role' => SystemRoles::SYSTEM_ADMIN), $website); $managerUserId = UserCommands::createUser(array('id' => '', 'name' => $constants['managerName'], 'email' => $constants['managerEmail'], 'username' => $constants['managerUsername'], 'password' => $constants['managerPassword'], 'active' => true, 'role' => SystemRoles::USER), $website); $memberUserId = UserCommands::createUser(array('id' => '', 'name' => $constants['memberName'], 'email' => $constants['memberEmail'], 'username' => $constants['memberUsername'], 'password' => $constants['memberPassword'], 'active' => true, 'role' => SystemRoles::USER), $website); $expiredUserId = UserCommands::createUser(array('id' => '', 'name' => $constants['expiredName'], 'email' => $constants['expiredEmail'], 'username' => $constants['expiredUsername'], 'password' => $constants['memberPassword'], 'active' => true, 'role' => SystemRoles::USER), $website); $resetUserId = UserCommands::createUser(array('id' => '', 'name' => $constants['resetName'], 'email' => $constants['resetEmail'], 'username' => $constants['resetUsername'], 'password' => $constants['memberPassword'], 'active' => true, 'role' => SystemRoles::USER), $website); // set forgot password with expired date $today = new DateTime(); $expiredUser = new UserModel($expiredUserId); $expiredUser->resetPasswordKey = $constants['expiredPasswordKey']; $expiredUser->resetPasswordExpirationDate = $today; $expiredUser->write(); // set forgot password with valid date $resetUser = new UserModel($resetUserId); $resetUser->resetPasswordKey = $constants['resetPasswordKey']; $resetUser->resetPasswordExpirationDate = $today->add(new DateInterval('P5D'));
/** * @param string $database * @param string $collection * @param string $idKey defaults to id */ public function __construct($database, $collection, $idKey = 'id') { $this->_db = MongoStore::connect($database); $this->_collection = $this->_db->{$collection}; $this->_idKey = $idKey; }