public function testSequentialProcessViewFactory() { Yii::app()->user->userModel = User::getByUsername('super'); $freezeWhenComplete = false; //Unfreeze since the test model is not part of the standard schema. if (RedBeanDatabase::isFrozen()) { RedBeanDatabase::unfreeze(); $freezeWhenComplete = true; } $testModels = ImportModelTestItem::getAll(); $this->assertEquals(0, count($testModels)); $import = new Import(); $mappingData = array('column_0' => array('attributeIndexOrDerivedType' => 'string', 'type' => 'importColumn', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => null))), 'column_23' => array('attributeIndexOrDerivedType' => 'FullName', 'type' => 'importColumn', 'mappingRulesData' => array('FullNameDefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => null)))); $serializedData['importRulesType'] = 'ImportModelTestItem'; $serializedData['mappingData'] = $mappingData; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('importAnalyzerTest.csv', $import->getTempTableName()); $config = array('pagination' => array('pageSize' => 2)); $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $sequentialProcess = new ImportCreateUpdateModelsSequentialProcess($import, $dataProvider); $sequentialProcess->run(null, null); $route = 'default/someAction'; $view = SequentialProcessViewFactory::makeBySequentialProcess($sequentialProcess, $route); $content = $view->render(); $this->assertNotNull($content); $this->assertEquals('SequentialProcessView', get_class($view)); $this->assertEquals('processRows', $sequentialProcess->getNextStep()); //Now process the first run. Will process page 0. $sequentialProcess = new ImportCreateUpdateModelsSequentialProcess($import, $dataProvider); $sequentialProcess->run('processRows', null); $route = 'default/someAction'; $view = SequentialProcessViewFactory::makeBySequentialProcess($sequentialProcess, $route); $content = $view->render(); $this->assertNotNull($content); $this->assertEquals('SequentialProcessView', get_class($view)); $this->assertEquals(array('page' => 1), $sequentialProcess->getNextParams()); //Confirm 2 models were successfully added. $testModels = ImportModelTestItem::getAll(); $this->assertEquals(2, count($testModels)); //Re-freeze if needed. if ($freezeWhenComplete) { RedBeanDatabase::freeze(); } }
public function testSettingExplicitReadWriteModelPermissionsDuringImportDontAffectTheModifiedByUserAttribute() { Yii::app()->user->userModel = User::getByUsername('super'); $testModels = ImportModelTestItem::getAll(); $this->assertEquals(0, count($testModels)); //Add a read only user for import. Then all models should be readable by jim in addition to super. $explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions(); $explicitReadWriteModelPermissions->addReadOnlyPermitable(User::getByUsername('jim')); $testModels = ImportModelTestItem::getAll(); $this->assertEquals(0, count($testModels)); $import = new Import(); $serializedData['importRulesType'] = 'ImportModelTestItem'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('importAnalyzerTest3.csv', $import->getTempTableName(), true); $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $mappingData = array('column_0' => array('attributeIndexOrDerivedType' => 'string', 'type' => 'importColumn', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => null))), 'column_7' => array('attributeIndexOrDerivedType' => 'modifiedByUser', 'type' => 'importColumn', 'mappingRulesData' => array('UserValueTypeModelAttributeMappingRuleForm' => array('type' => UserValueTypeModelAttributeMappingRuleForm::ZURMO_USERNAME))), 'column_23' => array('attributeIndexOrDerivedType' => 'FullName', 'type' => 'importColumn', 'mappingRulesData' => array('FullNameDefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => null)))); $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem'); $page = 0; $config = array('pagination' => array('pageSize' => 3)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, $explicitReadWriteModelPermissions, $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 1 models where created. $testModels = ImportModelTestItem::getAll(); $this->assertEquals(1, count($testModels)); $jim = User::getByUsername('jim'); foreach ($testModels as $model) { $this->assertEquals(array(Permission::READ, Permission::NONE), $model->getExplicitActualPermissions($jim)); $this->assertEquals('jim', $model->modifiedByUser->username); } //Clear out data in table ImportModelTestItem::deleteAll(); //Now test with read/write permissions being set. $explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions(); $explicitReadWriteModelPermissions->addReadWritePermitable(User::getByUsername('jim')); $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, $explicitReadWriteModelPermissions, $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 1 models where created. $testModels = ImportModelTestItem::getAll(); $this->assertEquals(1, count($testModels)); $jim = User::getByUsername('jim'); foreach ($testModels as $model) { $this->assertEquals(array(Permission::READ_WRITE_CHANGE_PERMISSIONS_CHANGE_OWNER, Permission::NONE), $model->getExplicitActualPermissions($jim)); $this->assertEquals('jim', $model->modifiedByUser->username); } }