public function testLogAuditForOwnedMultipleValuesCustomField() { Yii::app()->user->userModel = User::getByUsername('jimmy'); $beforeCount = AuditEvent::getCount(); $values = array('A', 'B', 'C', 'CC', 'CCC'); $customFieldData = CustomFieldData::getByName('MultipleIndustries'); $customFieldData->serializedData = serialize($values); $saved = $customFieldData->save(); $this->assertTrue($saved); $model = new TestOwnedCustomFieldsModel(); $this->assertTrue($model->save()); $this->assertEquals($beforeCount + 1, AuditEvent::getCount()); $model = TestOwnedCustomFieldsModel::getById($model->id); $value = new CustomFieldValue(); $value->value = 'C'; $model->multipleIndustries->values->removeAll(); //To remove the blank CustomFieldValue. This mimics //setValues($values) in MultipleValuesCustomField. $model->multipleIndustries->values->add($value); $this->assertTrue($model->save()); $this->assertEquals($beforeCount + 2, AuditEvent::getCount()); $model = TestOwnedCustomFieldsModel::getById($model->id); $value = new CustomFieldValue(); $value->value = 'B'; $model->multipleIndustries->values->add($value); $this->assertTrue($model->save()); $this->assertEquals($beforeCount + 3, AuditEvent::getCount()); $AuditEventsList = AuditEvent::getTailEvents(3); $this->assertRegExp('/[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+ [AP]M, ' . 'James Boondog, Item Created, ' . 'TestOwnedCustomFieldsModel\\([0-9]+\\), \\(None\\)/', ZurmoModule::stringifyAuditEvent($AuditEventsList[0])); $this->assertRegExp('/[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+ [AP]M, ' . 'James Boondog, Item Modified, ' . 'TestOwnedCustomFieldsModel\\([0-9]+\\), \\(None\\), ' . 'Changed Multiple Industries Values from to C/', ZurmoModule::stringifyAuditEvent($AuditEventsList[1])); $this->assertRegExp('/[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+ [AP]M, ' . 'James Boondog, Item Modified, ' . 'TestOwnedCustomFieldsModel\\([0-9]+\\), \\(None\\), ' . 'Changed Multiple Industries Values from C to C, B/', ZurmoModule::stringifyAuditEvent($AuditEventsList[2])); }
/** * @depends testSearchByMultipleValuesCustomField */ public function testSearchByTwoMultipleValuesCustomField() { if (!RedBeanDatabase::isFrozen()) { $quote = DatabaseCompatibilityUtil::getQuote(); //Test where relatioon id is in a joining table. Many to Many relationship $_FAKEPOST['TestOwnedCustomFieldsModel'] = array(); $_FAKEPOST['TestOwnedCustomFieldsModel']['multipleIndustries']['values'] = array('A', 'B', 'C'); $_FAKEPOST['TestOwnedCustomFieldsModel']['multipleSomethings']['values'] = array('D', 'E', 'F'); $metadataAdapter = new SearchDataProviderMetadataAdapter(new TestOwnedCustomFieldsModel(false), 1, $_FAKEPOST['TestOwnedCustomFieldsModel']); $searchAttributeData = $metadataAdapter->getAdaptedMetadata(); $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestOwnedCustomFieldsModel'); $where = RedBeanModelDataProvider::makeWhere('TestOwnedCustomFieldsModel', $searchAttributeData, $joinTablesAdapter); $compareWhere = "(1 = (select 1 from {$quote}customfieldvalue{$quote} customfieldvalue " . "where {$quote}customfieldvalue{$quote}.{$quote}multiplevaluescustomfield_id{$quote} = " . "{$quote}multiplevaluescustomfield{$quote}.id " . "and {$quote}customfieldvalue{$quote}.{$quote}value{$quote} IN('A','B','C') limit 1))"; // Not Coding Standard $compareWhere .= " and (1 = (select 1 from {$quote}customfieldvalue{$quote} customfieldvalue " . "where {$quote}customfieldvalue{$quote}.{$quote}multiplevaluescustomfield_id{$quote} = " . "{$quote}multiplevaluescustomfield1{$quote}.id " . "and {$quote}customfieldvalue{$quote}.{$quote}value{$quote} IN('D','E','F') limit 1))"; // Not Coding Standard $this->assertEquals($compareWhere, $where); //Now test that the joinTablesAdapter has correct information. $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount()); $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount()); $leftTables = $joinTablesAdapter->getLeftTablesAndAliases(); $this->assertEquals('multiplevaluescustomfield', $leftTables[0]['tableName']); $this->assertEquals('multiplevaluescustomfield', $leftTables[1]['tableName']); //Now test that the subsetSQL query produced is correct. $subsetSql = TestOwnedCustomFieldsModel::makeSubsetOrCountSqlQuery('testcustomfieldsmodel', $joinTablesAdapter, 1, 5, $where, null); $compareSubsetSql = "select {$quote}testcustomfieldsmodel{$quote}.{$quote}id{$quote} id "; $compareSubsetSql .= "from {$quote}testcustomfieldsmodel{$quote} "; $compareSubsetSql .= "left join {$quote}multiplevaluescustomfield{$quote} on "; $compareSubsetSql .= "{$quote}multiplevaluescustomfield{$quote}.{$quote}id{$quote} = "; $compareSubsetSql .= "{$quote}testownedcustomfieldsmodel{$quote}.{$quote}multipleindustries_multiplevaluescustomfield_id{$quote} "; $compareSubsetSql .= "left join {$quote}multiplevaluescustomfield{$quote} multiplevaluescustomfield1 on "; $compareSubsetSql .= "{$quote}multiplevaluescustomfield1{$quote}.{$quote}id{$quote} = "; $compareSubsetSql .= "{$quote}testownedcustomfieldsmodel{$quote}.{$quote}multiplesomethings_multiplevaluescustomfield_id{$quote} "; $compareSubsetSql .= "where " . $compareWhere . ' '; $compareSubsetSql .= 'limit 5 offset 1'; $this->assertEquals($compareSubsetSql, $subsetSql); //Make sure the sql runs properly. $dataProvider = new RedBeanModelDataProvider('TestOwnedCustomFieldsModel', null, false, $searchAttributeData); $data = $dataProvider->getData(); $this->assertEquals(1, count($data)); } }