public function testDownCasts() { $possibleDerivationPaths = array(array('SecurableItem', 'OwnedSecurableItem', 'Account'), array('SecurableItem', 'OwnedSecurableItem', 'Person', 'Contact'), array('SecurableItem', 'OwnedSecurableItem', 'Opportunity')); $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $account = AccountTestHelper::createAccountByNameForOwner('Waggle', $super); $contact = ContactTestHelper::createContactByNameForOwner('Noddy', $super); $opportunity = OpportunityTestHelper::createOpportunityByNameForOwner('Noddy', $super); $accountItem = Item::getById($account->getClassId('Item')); $contactItem = Item::getById($contact->getClassId('Item')); $opportunityItem = Item::getById($opportunity->getClassId('Item')); $this->assertTrue($accountItem->isSame($account)); $this->assertTrue($contactItem->isSame($contact)); $this->assertTrue($opportunityItem->isSame($opportunity)); $this->assertFalse($accountItem instanceof Account); $this->assertFalse($contactItem instanceof Contact); $this->assertFalse($opportunityItem instanceof Opportunity); $account2 = $accountItem->castDown($possibleDerivationPaths); $this->assertEquals('Account', get_class($account2)); //Demonstrate a single array, making sure it casts down properly. $accountItem2 = Item::getById($account->getClassId('Item')); $account3 = $accountItem2->castDown(array(array('SecurableItem', 'OwnedSecurableItem', 'Account'))); $this->assertEquals('Account', get_class($account3)); $contact2 = $contactItem->castDown($possibleDerivationPaths); $opportunity2 = $opportunityItem->castDown($possibleDerivationPaths); $this->assertTrue($account2->isSame($account)); $this->assertTrue($contact2->isSame($contact)); $this->assertTrue($opportunity2->isSame($opportunity)); $this->assertTrue($account2 instanceof Account); $this->assertTrue($contact2 instanceof Contact); $this->assertTrue($opportunity2 instanceof Opportunity); $account2 = AccountTestHelper::createAccountByNameForOwner('Waggle2', $super); //By adding a second contact with a relation to the account2, we can demonstrate a bug with how castDown works. //Since contacts can in fact be attached to accounts via account_id, if a contact exists connected to the account //we are trying to cast down, then this will cast down even though it shouldn't. $contact2 = ContactTestHelper::createContactWithAccountByNameForOwner('MrWaggle2', $super, $account2); try { $account2CastedDown = $account2->castDown(array(array('SecurableItem', 'OwnedSecurableItem', 'Person', 'Contact'))); $this->fail(); } catch (NotFoundException $e) { //success } //Now try to forget the account and retrieve it. $account2Id = $account2->id; $account2->forget(); unset($account2); $account2 = Account::getById($account2Id); try { $account2CastedDown = $account2->castDown(array(array('SecurableItem', 'OwnedSecurableItem', 'Person', 'Contact'))); $this->fail(); } catch (NotFoundException $e) { //success } }
/** * Based on the post data, resolve the conversation participants. While this is being resolved also * resolve the correct read/write permissions. * @param Conversation $conversation * @param array $postData * @param object $explicitReadWriteModelPermissions - ExplicitReadWriteModelPermissions model * @return Array of persons who have been added as participants */ public static function resolveConversationHasManyParticipantsFromPost(Conversation $conversation, $postData, $explicitReadWriteModelPermissions) { assert('$explicitReadWriteModelPermissions instanceof ExplicitReadWriteModelPermissions'); $newPeopleIndexedByItemId = array(); if (isset($postData['itemIds']) && strlen($postData['itemIds']) > 0) { $itemIds = explode(",", $postData['itemIds']); // Not Coding Standard foreach ($itemIds as $itemId) { if ($itemId != $conversation->owner->getClassId('Item')) { $newPeopleIndexedByItemId[$itemId] = static::castDownItem(Item::getById((int) $itemId)); } } if ($conversation->conversationParticipants->count() > 0) { $participantsToRemove = array(); foreach ($conversation->conversationParticipants as $index => $existingParticipantModel) { if (!isset($newPeopleIndexedByItemId[$existingParticipantModel->person->getClassId('Item')])) { $participantsToRemove[] = $existingParticipantModel; } else { unset($newPeopleIndexedByItemId[$existingParticipantModel->person->getClassId('Item')]); } } foreach ($participantsToRemove as $participantModelToRemove) { $conversation->conversationParticipants->remove($participantModelToRemove); $person = static::castDownItem($participantModelToRemove->person); if ($person instanceof Permitable) { $explicitReadWriteModelPermissions->addReadWritePermitableToRemove($person); } } } //Now add missing participants foreach ($newPeopleIndexedByItemId as $personOrUserModel) { $conversation->conversationParticipants->add(static::makeConversationParticipantByPerson($personOrUserModel)); if ($personOrUserModel instanceof Permitable) { $explicitReadWriteModelPermissions->addReadWritePermitable($personOrUserModel); } } } else { //remove all participants $conversation->conversationParticipants->removeAll(); $explicitReadWriteModelPermissions->removeAllReadWritePermitables(); } return $newPeopleIndexedByItemId; }
/** * Should not throw exception, should cast down ok * @depends testWebsiteCanBeSavedWithoutUrlScheme */ public function testCastDown() { $user = User::getByUsername('steven'); $account = new Account(); $account->owner = $user; $account->name = 'Account'; $this->assertTrue($account->save()); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Account'); $itemId = $account->getClassId('Item'); $item = Item::getById($itemId); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); $this->assertEquals('Account', get_class($castedDownModel)); }
/** * Should not throw exception, should cast down ok */ public function testCastDown() { Yii::app()->user->userModel = User::getByUsername('super'); $startStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $meeting = new Meeting(); $meeting->name = 'aTest'; $meeting->startDateTime = $startStamp; $this->assertTrue($meeting->save()); $itemId = $meeting->getClassId('Item'); $item = Item::getById($itemId); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Meeting'); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); $this->assertEquals('Meeting', get_class($castedDownModel)); }
public function testManyToManyRelationInTheMiddleOfTheInheritanceHierarchy() { Yii::app()->user->userModel = User::getByUsername('super'); $accounts = Account::getByName('anAccount'); $possibleDerivationPaths = array(array('SecurableItem', 'OwnedSecurableItem', 'Account'), array('SecurableItem', 'OwnedSecurableItem', 'Person', 'Contact'), array('SecurableItem', 'OwnedSecurableItem', 'Opportunity')); $model = new TestManyManyRelationToItemModel(); $model->items->add($accounts[0]); $this->assertTrue($model->save()); $item = Item::getById($model->items[0]->getClassId('Item')); $this->assertTrue($item instanceof Item); $this->assertFalse($item instanceof Account); $this->assertTrue($item->isSame($accounts[0])); $account2 = $item->castDown($possibleDerivationPaths); $this->assertTrue($account2->isSame($accounts[0])); $id = $model->id; unset($model); RedBeanModel::forgetAll(); $model = TestManyManyRelationToItemModel::getById($id); $this->assertEquals(1, $model->items->count()); $this->assertTrue($model->items[0] instanceof Item); $this->assertFalse($model->items[0] instanceof Account); $this->assertTrue($model->items[0]->isSame($accounts[0])); $account3 = $model->items[0]->castDown($possibleDerivationPaths); $this->assertTrue($account3->isSame($accounts[0])); }
public function update() { $item = new Item($this->db); if ($this->f3->exists('POST.update')) { $item->edit($this->f3->get('POST.tok')); //get id getIdByTok $getIdByTok = new Item($this->db); $getIdByTok->getIdByTok($this->f3->get('POST.tok')); $iid = $this->f3->get('ID.id'); //del tags $tgs = new Tag2Item($this->db); $tgs->getByItemId($iid); if (count($tgs->getByItemId($iid)) > 0) { foreach ($tgs->getByItemId($iid) as $t) { $itemid = $t['id']; $delti = new Tag2Item($this->db); $delti->delete($itemid); } } //update tags $tags = explode(',', $this->f3->get('POST.tags')); foreach ($tags as $t) { $t = trim($t); if ($t != '') { $this->f3->clear('TAGS'); $ifexists = new Tag($this->db); $ifexists->getByName(strtolower($t)); if ($this->f3->exists('TAGS.id')) { $tid = $this->f3->get('TAGS.id'); } else { //insert new tag $newtag = new Tag($this->db); $newtag->title = strtolower(preg_replace('|[^0-9a-z \\-\\/+]|', '', $t)); $newtag->label = preg_replace('|[^0-9A-Za-z \\-\\/+]|', '', $t); $newtag->url = toUrl($t); //get unique tok $utok = new Tag($this->db); $randtok = rand(100000000, 999999999); while ($utok->tagcountByTok($randtok) > 0) { $randtok = rand(100000000, 999999999); } $newtag->tok = $randtok; $newtag->add(); $tid = $newtag->_id; } //add to Tag2Item $t2i = new Tag2Item($this->db); //insert lastinsertedid $t2i->tid = $tid; $t2i->iid = $iid; $t2i->add(); } } $this->f3->set('COOKIE.message', 'The bookmark has been successfully saved!'); $this->f3->set('COOKIE.messagetype', 'alert-success hide5s'); $this->f3->reroute('/'); } else { $item->getById($this->f3->get('PARAMS.tok')); $this->f3->set('item', $item); $this->f3->set('active', $this->f3->get('PARAMS.tok')); $this->f3->set('page_head', 'Update Item'); //template $this->f3->set('view', 'item/update.htm'); $tgs = new TagList($this->db); $this->f3->set('tgs', $tgs->getitemtags($this->f3->get('POST.tok'))); $this->f3->set('ctrcount', count($tgs->getitemtags($this->f3->get('POST.tok')))); //menu $this->f3->set('topmenu', 'i'); //breadcrumbs $this->f3->set('breadcrumb', array(array("url" => NULL, "name" => "Update bookmark"))); } }