diff --git a/code/extensions/TaggingExtension.php b/code/extensions/TaggingExtension.php index 4c4adcd..0e54b52 100644 --- a/code/extensions/TaggingExtension.php +++ b/code/extensions/TaggingExtension.php @@ -118,9 +118,9 @@ public function onBeforeWrite() { foreach($types as $relationship => $type) { foreach($this->owner->$relationship() as $tag) { - // Update both the fusion tags and tagging. + // Update both the fusion tags and tagging (using the relationship causes caching issues). - $fusion = $tag->FusionTag(); + $fusion = FusionTag::get()->byID($tag->FusionTagID); $this->owner->FusionTags()->add($fusion); $tagging[] = $fusion->Title; } diff --git a/tests/FusionUnitTests.php b/tests/FusionUnitTests.php index f632712..c9279fe 100644 --- a/tests/FusionUnitTests.php +++ b/tests/FusionUnitTests.php @@ -78,6 +78,79 @@ public function testTags() { $fusion = FusionTag::get()->byID($ID); $this->assertTrue(is_object($fusion)); $this->assertEquals($fusion->TagTypes, null); + + // The database needs to be emptied to prevent further testing conflict. + + self::empty_temp_db(); + } + + /** + * The test to ensure the page tagging is functioning correctly. + */ + + public function testTagging() { + + // Instantiate a page to use. + + $page = SiteTree::create(); + + // Determine whether consolidated tags are found in the existing relationships. + + $types = array(); + $existing = singleton('FusionService')->getFusionTagTypes(); + foreach($existing as $type => $field) { + $types[$type] = $type; + } + $types = array_intersect($page->many_many(), $types); + if(empty($types)) { + + // Instantiate a tag to use, adding it against the page. + + $tag = FusionTag::create(); + $field = 'Title'; + $tag->$field = 'new'; + $tag->write(); + $page->FusionTags()->add($tag->ID); + } + else { + + // There are consolidated tags found. + + foreach($types as $relationship => $type) { + + // Instantiate a tag to use, adding it against the page. + + $tag = $type::create(); + $field = $existing[$type]; + $tag->$field = 'new'; + $tag->write(); + $page->$relationship()->add($tag->ID); + + // The consolidated tags are automatically parsed, so this only needs to exist against one. + + break; + } + } + $page->writeToStage('Stage'); + $page->writeToStage('Live'); + + // Determine whether the page tagging reflects this. + + $this->assertContains($tag->$field, $page->Tagging); + + // Update the tag. + + $tag->$field = 'changed'; + $tag->write(); + + // Determine whether the page tagging reflects this. + + $page = SiteTree::get()->byID($page->ID); + $this->assertContains($tag->$field, $page->Tagging); + + // The database needs to be emptied to prevent further testing conflict. + + self::empty_temp_db(); } }