public function testSavingTwiceWithAModelThatHasACurrencyValueAsARelation() { if (!RedBeanDatabase::isFrozen()) { Yii::app()->user->userModel = User::getByUsername('super'); $testItem = new OwnedSecurableTestItem(); $testItem->member = 'test'; $saved = $testItem->save(); $this->assertTrue($saved); //Because OwnedSecurableTestItem as a relatedCurrency, there are some strange issues with saving again. //It creates currency validation issues for any of the related users like owner, modifiedUser etc. //Need to investigate further to fix. //$testItem->forget(); //$testItem = OwnedSecurableTestItem::getById($testItem->id); //Save again immediately after. $validated = $testItem->validate(); // echo "<pre>"; // print_r($testItem->getErrors()); // echo "</pre>"; $this->assertTrue($validated); $saved = $testItem->save(); $this->assertTrue($saved); //Reset count of test items to 0. $testItem->delete(); } }
public function testOwnerChangeChangesModifiedDateTime() { Yii::app()->user->userModel = User::getByUsername('super'); $user = UserTestHelper::createBasicUser('basic'); $testItem = new OwnedSecurableTestItem(); $testItem->member = 'test'; $this->assertTrue($testItem->save()); $defaultDateTimeModified = $testItem->modifiedDateTime; sleep(1); $testItem->owner = $user; $this->assertTrue($testItem->save()); $testItemId = $testItem->id; $testItem->forget(); $testItem = OwnedSecurableTestItem::getById($testItemId); $this->assertNotEquals($defaultDateTimeModified, $testItem->modifiedDateTime); }
public function testUnionQuery() { //regular user no elevation $user = new User(); $user->username = '******'; $user->firstName = 'aaa'; $user->lastName = 'aaa'; $saved = $user->save(); $this->assertTrue($saved); $aUser = User::getByUsername('aaa'); Yii::app()->user->userModel = $aUser; $mungeIds = AllPermissionsOptimizationUtil::getMungeIdsByUser($aUser); $model = new OwnedSecurableTestItem(); $model->member = 'test4'; $model->owner = $aUser; $this->assertTrue($model->save()); Yii::app()->user->userModel = User::getByUsername('super'); $this->assertEquals(4, OwnedSecurableTestItem::getCount()); $model = new OwnedSecurableTestItem2(); $model->member = 'test4'; $model->owner = $aUser; $this->assertTrue($model->save()); $this->assertEquals(2, OwnedSecurableTestItem2::getCount()); Yii::app()->user->userModel = User::getByUsername('aaa'); $quote = DatabaseCompatibilityUtil::getQuote(); //Test search attribute data across multiple models. $aFakePost = array('member' => 'test4'); $aMetadataAdapter = new SearchDataProviderMetadataAdapter(new OwnedSecurableTestItem(false), $aUser->id, $aFakePost); $bFakePost = array('member' => 'test4'); $bMetadataAdapter = new SearchDataProviderMetadataAdapter(new OwnedSecurableTestItem2(false), $aUser->id, $bFakePost); $modelClassNamesAndSearchAttributeData = array(array('OwnedSecurableTestItem' => $aMetadataAdapter->getAdaptedMetadata()), array('OwnedSecurableTestItem2' => $bMetadataAdapter->getAdaptedMetadata())); $unionSql = RedBeanModelsDataProvider::makeUnionSql($modelClassNamesAndSearchAttributeData, null, false, 2, 7); $compareWhere = "({$quote}ownedsecurabletestitem{$quote}.{$quote}member{$quote} like 'test4%')"; $compareWhere2 = "({$quote}ownedsecurabletestitem2{$quote}.{$quote}member{$quote} like 'test4%')"; $compareSubsetSql = "("; $compareSubsetSql .= "select distinct {$quote}ownedsecurabletestitem{$quote}.{$quote}id{$quote} id"; $compareSubsetSql .= ", 'OwnedSecurableTestItem' modelClassName "; $compareSubsetSql .= "from ({$quote}ownedsecurabletestitem{$quote}, {$quote}ownedsecurableitem{$quote}) "; $compareSubsetSql .= "left join {$quote}ownedsecurabletestitem_read{$quote} on "; $compareSubsetSql .= "{$quote}ownedsecurabletestitem_read{$quote}.{$quote}securableitem_id{$quote} = "; $compareSubsetSql .= "{$quote}ownedsecurableitem{$quote}.{$quote}securableitem_id{$quote} "; $compareSubsetSql .= "and {$quote}munge_id{$quote} in ('" . join("', '", $mungeIds) . "') "; $compareSubsetSql .= "where ({$compareWhere}) and ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = {$aUser->id} "; $compareSubsetSql .= "OR {$quote}ownedsecurabletestitem_read{$quote}.{$quote}munge_id{$quote} IS NOT NULL) "; // Not Coding Standard $compareSubsetSql .= "and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = "; $compareSubsetSql .= "{$quote}ownedsecurabletestitem{$quote}.{$quote}ownedsecurableitem_id{$quote}"; $compareSubsetSql .= ") "; $compareSubsetSql .= "UNION ("; $compareSubsetSql .= "select distinct {$quote}ownedsecurabletestitem2{$quote}.{$quote}id{$quote} id"; $compareSubsetSql .= ", 'OwnedSecurableTestItem2' modelClassName "; $compareSubsetSql .= "from ({$quote}ownedsecurabletestitem2{$quote}, {$quote}ownedsecurableitem{$quote}) "; $compareSubsetSql .= "left join {$quote}ownedsecurabletestitem2_read{$quote} on "; $compareSubsetSql .= "{$quote}ownedsecurabletestitem2_read{$quote}.{$quote}securableitem_id{$quote} = "; $compareSubsetSql .= "{$quote}ownedsecurableitem{$quote}.{$quote}securableitem_id{$quote} "; $compareSubsetSql .= "and {$quote}munge_id{$quote} in ('" . join("', '", $mungeIds) . "') "; $compareSubsetSql .= "where ({$compareWhere2}) and ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = {$aUser->id} "; $compareSubsetSql .= "OR {$quote}ownedsecurabletestitem2_read{$quote}.{$quote}munge_id{$quote} IS NOT NULL) "; // Not Coding Standard $compareSubsetSql .= "and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = "; $compareSubsetSql .= "{$quote}ownedsecurabletestitem2{$quote}.{$quote}ownedsecurableitem_id{$quote}"; $compareSubsetSql .= ") "; $compareSubsetSql .= 'limit 7 offset 2'; $this->assertEquals($compareSubsetSql, $unionSql); //Make sure the sql runs properly. $dataProvider = new RedBeanModelsDataProvider('anId', null, false, $modelClassNamesAndSearchAttributeData); $data = $dataProvider->getData(); //Test results are correct $this->assertEquals(2, count($data)); $this->assertEquals('OwnedSecurableTestItem', get_class($data[0])); $this->assertEquals('OwnedSecurableTestItem2', get_class($data[1])); //Make sure union count query produces the same count. $this->assertEquals(2, $dataProvider->calculateTotalItemCount()); }