/** * @depends testInstall */ public function testTags() { $object_id = 2; //1. Add TAG taxonomy $term = new TaxonomyDef(); $term->name = 'test_tag'; $term->class = TagTerm::className(); $term->data_table = 'sample_tags'; $term->ref_table = SampleTable::className(); //2. Create data table $tagTerm = Yii::createObject($term->attributes); $migration = $tagTerm->install(); $this->runMigration($migration); $this->tester->assertTrue($this->getTaxonomy()->getTerm($term->name)->isInstalled(), 'The term should be installed.'); //3. Add some data $this->getTaxonomy()->addTerm($term->name, $object_id, ['tag1', 'tag2']); $term = $this->getTaxonomy()->getTerm($term->name, true); //check count on term $this->tester->assertEquals(2, $term->total_count, "Tag term count not correct ({$term->total_count})!"); $data = $this->getTaxonomy()->getTerms($term->name, $object_id); // tag1 + tag2 $this->tester->assertEquals(2, count($data), 'Tag term count not correct!'); $this->tester->assertContains('tag1', $data, 'Tag1 missing in data'); $this->tester->assertContains('tag2', $data, 'Tag1 missing in data'); $this->getTaxonomy()->removeTerm($term->name, $object_id, ['name' => 'tag1']); $data = $this->getTaxonomy()->getTerms($term->name, $object_id); // tag1 + tag2 $this->tester->assertEquals(1, count($data), 'Tag term count not correct!'); $this->tester->assertNotContains('tag1', $data, 'Tag1 present in data'); $this->tester->assertContains('tag2', $data, 'Tag1 missing in data'); $this->getTaxonomy()->removeTerm($term->name, $object_id); $data = $this->getTaxonomy()->getTerms($term->name, $object_id); // tag1 + tag2 $this->tester->assertEmpty($data, 'Tag term data not correct!'); $this->tester->assertNotContains('tag1', $data, 'Tag1 present in data'); // 4. setTerms() test $term->addTerm($object_id, ['tag1', 'tag2']); // Add new terms $term->setTerms($object_id, ['tag1', 'tag3', 'tag4']); // Overwrite all terms of this object $data = $term->getTerms($object_id); $this->tester->assertEquals(3, count($data), 'Wrong term count!'); $this->tester->assertContains('tag1', $data, 'Tag1 missing in data'); $this->tester->assertContains('tag3', $data, 'Tag3 missing in data'); $this->tester->assertContains('tag4', $data, 'Tag4 missing in data'); $this->tester->assertNotContains('tag2', $data, 'Tag2 must not be present in data'); // 5. getTerms test $data = $term->getTerms($object_id); // Get all terms of the given object $this->tester->assertEquals(3, count($data), 'Wrong term count!'); $data = $term->getTerms(); // Get all terms currently present in the system $this->tester->assertEquals(4, count($data), 'Wrong term count!'); }
/** * @return array */ public function getDefinitions() { return array_merge([TagTerm::className(), PropertyTerm::className(), CategoryTerm::className()], $this->definitions); }