public function testRebuildDatabaseWithNonSuperAdminUser() { $steven = UserTestHelper::createBasicUser('Steven'); Yii::app()->user->userModel = $steven; $item = NamedSecurableItem::getByName('AccountsModule'); $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($steven)); $accounts = Account::getAll(); $users = User::getAll(); $this->assertEquals(0, count($accounts)); $this->assertEquals(2, count($users)); $adapter = new ModelAttributesAdapter(new Account()); $adapter->resolveDatabaseSchemaForModel('Account'); //Confirm the counts of data are still correct $accounts = Account::getAll(); $users = User::getAll(); $this->assertEquals(0, count($accounts)); $this->assertEquals(2, count($users)); //Explicitly deny read, write, and deny Yii::app()->user->userModel = User::getByUsername('super'); $item->addPermissions($steven, Permission::READ_WRITE_DELETE, Permission::DENY); $this->assertTrue($item->save()); //Make sure steven has explicit deny $item = NamedSecurableItem::getByName('AccountsModule'); $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($steven)); Yii::app()->user->userModel = $steven; $adapter = new ModelAttributesAdapter(new Account()); $adapter->resolveDatabaseSchemaForModel('Account'); //Confirm the counts of data are still correct $accounts = Account::getAll(); $users = User::getAll(); $this->assertEquals(0, count($accounts)); $this->assertEquals(2, count($users)); }
/** * There was a bug if you had an existing model, then created a custom drop down, it would not show * any values. This was resolved by making sure cached models constructDerived. This test should pass now * that the fix is implemented. * @depends testGetRequiredDerivedLayoutAttributeTypes */ public function testExistingModelsShowCustomFieldDataCorrectlyWhenAttributeIsAddedAsDatabaseColumn() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; //Create account $account = AccountTestHelper::createAccountByNameForOwner('test', $super); $accountId = $account->id; $account->forget(); $originalMetadata = Account::getMetadata(); $attributeLabels = array('en' => 'newRelation'); ModelMetadataUtil::addOrUpdateCustomFieldRelation('Account', 'newRelation', $attributeLabels, null, false, false, 'DropDown', 'Things', array('thing 1', 'thing 2'), array('fr' => array('thing 1 fr', 'thing 2 fr'))); $adapter = new ModelAttributesAdapter(new Account()); $adapter->resolveDatabaseSchemaForModel('Account'); $metadata = Account::getMetadata(); $this->assertNotEquals($originalMetadata, $metadata); $this->assertEquals($originalMetadata['Account']['rules'], $metadata['Account']['rules']); $newRelation = $metadata['Account']['relations']['newRelationCstm']; $this->assertEquals(array(RedBeanModel::HAS_ONE, 'OwnedCustomField', RedBeanModel::OWNED, RedBeanModel::LINK_TYPE_SPECIFIC, 'newRelationCstm'), $newRelation); $this->assertEquals('Things', $metadata['Account']['customFields']['newRelationCstm']); //on a new account, does the serialized data show correctly. $account = new Account(); $this->assertEquals(array('thing 1', 'thing 2'), unserialize($account->newRelationCstm->data->serializedData)); ForgetAllCacheUtil::forgetAllCaches(); //retrieve account and make sure the serialized data shows correctly. //This will not be cached. $account = Account::getById($accountId); $this->assertNotNull($account->industry->data->serializedData); $this->assertEquals(array('thing 1', 'thing 2'), unserialize($account->newRelationCstm->data->serializedData)); //This will pull from cached. Clear the php cache first, which simulates a new page request without destroying //the persistent cache. RedBeanModelsCache::forgetAll(true); $account = Account::getById($accountId); //Test pulling a different CustomField first. This simulates caching the customField $this->assertEquals(array('thing 1', 'thing 2'), unserialize($account->newRelationCstm->data->serializedData)); }