Source of file TagFieldTest.php
Size: 13,462 Bytes - Last Modified: 2021-12-23T10:35:23+00:00
/var/www/docs.ssmods.com/process/src/tests/TagFieldTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 | <?php namespace SilverStripe\TagField\Tests; use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\TagField\ReadonlyTagField; use SilverStripe\TagField\TagField; use SilverStripe\TagField\Tests\Stub\TagFieldTestBlogPost; use SilverStripe\TagField\Tests\Stub\TagFieldTestBlogTag; use SilverStripe\TagField\Tests\Stub\TagFieldTestController; class TagFieldTest extends SapphireTest { /** * @var string */ protected static $fixture_file = 'TagFieldTest.yml'; /** * @var array */ protected static $extra_dataobjects = [ TagFieldTestBlogTag::class, TagFieldTestBlogPost::class, ]; public function testItSavesLinksToNewTagsOnNewRecords() { $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); $field->setValue(['Tag3', 'Tag4']); $field->saveInto($record); $record->write(); $this->compareExpectedAndActualTags( ['Tag3', 'Tag4'], $record ); } /** * @param string $name * * @return TagFieldTestBlogPost */ protected function getNewTagFieldTestBlogPost($name) { return $this->objFromFixture( TagFieldTestBlogPost::class, $name ); } /** * @param array $expected * @param TagFieldTestBlogPost $record */ protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) { $this->compareTagLists($expected, $record->Tags()); } /** * Ensure a source of tags matches the given string tag names * * @param array $expected * @param DataList $actualSource */ protected function compareTagLists(array $expected, DataList $actualSource) { $actual = array_values($actualSource->map('ID', 'Title')->toArray()); sort($expected); sort($actual); $this->assertEquals( $expected, $actual ); } public function testItSavesLinksToNewTagsOnExistingRecords() { $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); $record->write(); $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); $field->setValue(['Tag3', 'Tag4']); $field->saveInto($record); $this->compareExpectedAndActualTags( array('Tag3', 'Tag4'), $record ); } public function testItSavesLinksToExistingTagsOnNewRecords() { $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); $field->setValue(['Tag1', 'Tag2']); $field->saveInto($record); $record->write(); $this->compareExpectedAndActualTags( ['Tag1', 'Tag2'], $record ); } public function testItSavesLinksToExistingTagsOnExistingRecords() { $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); $record->write(); $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); $field->setValue(['Tag1', 'Tag2']); $field->saveInto($record); $this->compareExpectedAndActualTags( ['Tag1', 'Tag2'], $record ); } public function testSavesReactTags() { $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); $record->write(); $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); $field->setValue([ [ 'Title' => 'Tag1', 'Value' => 'Tag1', ], [ 'Title' => 'Tag2', 'Value' => 'Tag2', ], ]); $field->saveInto($record); $this->compareExpectedAndActualTags( ['Tag1', 'Tag2'], $record ); } /** * Ensure that {@see TagField::saveInto} respects existing tags */ public function testSaveDuplicateTags() { $record = $this->getNewTagFieldTestBlogPost('BlogPost2'); $record->write(); $tag2ID = $this->idFromFixture(TagFieldTestBlogTag::class, 'Tag2'); // Check tags before write $this->compareExpectedAndActualTags( ['Tag1', '222'], $record ); $this->compareTagLists( ['Tag1', '222'], TagFieldTestBlogTag::get() ); $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); // Write new tags $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); $field->setValue(['222', 'Tag3']); $field->saveInto($record); // Check only one new tag was added $this->compareExpectedAndActualTags( ['222', 'Tag3'], $record ); // Ensure that only one new dataobject was added and that tag2s id has not changed $this->compareTagLists( ['Tag1', '222', 'Tag3'], TagFieldTestBlogTag::get() ); $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); } public function testItSuggestsTags() { $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); /** * Partial tag title match. */ $request = $this->getNewRequest(['term' => 'Tag']); $this->assertEquals( '{"items":[{"Title":"Tag1","Value":"Tag1"}]}', $field->suggest($request)->getBody() ); /** * Exact tag title match. */ $request = $this->getNewRequest(['term' => '222']); $this->assertEquals( '{"items":[{"Title":"222","Value":"222"}]}', $field->suggest($request)->getBody() ); /** * Case-insensitive tag title match. */ $request = $this->getNewRequest(['term' => 'TAG1']); $this->assertEquals( '{"items":[{"Title":"Tag1","Value":"Tag1"}]}', $field->suggest($request)->getBody() ); /** * No tag title match. */ $request = $this->getNewRequest(['term' => 'unknown']); $this->assertEquals( '{"items":[]}', $field->suggest($request)->getBody() ); } /** * Tests that TagField supports pre-filtered data sources */ public function testRestrictedSuggestions() { $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); $field = new TagField('Tags', '', $source); /** * Partial tag title match. */ $request = $this->getNewRequest(['term' => 'Tag']); $this->assertEquals( '{"items":[{"Title":"Tag1","Value":"Tag1"}]}', $field->suggest($request)->getBody() ); /** * Exact tag title match. */ $request = $this->getNewRequest(['term' => 'Tag1']); $this->assertEquals( '{"items":[{"Title":"Tag1","Value":"Tag1"}]}', $field->suggest($request)->getBody() ); /** * Excluded item doesn't appear in matches */ $request = $this->getNewRequest(['term' => 'Tag2']); $this->assertEquals( '{"items":[]}', $field->suggest($request)->getBody() ); } /** * @param array $parameters * * @return HTTPRequest */ protected function getNewRequest(array $parameters) { return new HTTPRequest( 'get', 'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest', $parameters ); } public function testItDisplaysValuesFromRelations() { $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); $record->write(); $form = new Form( new TagFieldTestController(), 'Form', new FieldList( $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)) ), new FieldList() ); $form->loadDataFrom( $this->objFromFixture(TagFieldTestBlogPost::class, 'BlogPost2') ); $ids = TagFieldTestBlogTag::get()->column('Title'); $this->assertEquals($field->Value(), $ids); } public function testItIgnoresNewTagsIfCannotCreate() { $this->markTestSkipped( 'This test has not been updated yet.' ); $record = new TagFieldTestBlogPost(); $record->write(); $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first(); $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class), [$tag->Title, 'Tag3']); $field->setCanCreate(false); $field->saveInto($record); /** * @var TagFieldTestBlogPost $record */ $record = DataObject::get_by_id(TagFieldTestBlogPost::class, $record->ID); $this->compareExpectedAndActualTags( ['Tag1'], $record ); } /** * Test you can save without a source set */ public function testSaveEmptySource() { $record = new TagFieldTestBlogPost(); $record->write(); // Clear database of tags TagFieldTestBlogTag::get()->removeAll(); $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); $field->setValue(['New Tag']); $field->setCanCreate(true); $field->saveInto($record); $tag = TagFieldTestBlogTag::get()->first(); $this->assertNotEmpty($tag); $this->assertEquals('New Tag', $tag->Title); $record = TagFieldTestBlogPost::get()->byID($record->ID); $this->assertEquals( $tag->ID, $record->Tags()->first()->ID ); } /** * Test read only fields are returned */ public function testReadonlyTransformation() { $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); $readOnlyField = $field->performReadonlyTransformation(); $this->assertInstanceOf(ReadonlyTagField::class, $readOnlyField); // Custom title field $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); $field->setTitleField('Name'); $readOnlyField = $field->performReadonlyTransformation(); $this->assertEquals('Name', $readOnlyField->getTitleField()); } public function testItDisplaysWithSelectedValuesFromDataList() { $source = TagFieldTestBlogTag::get(); $selectedTag = $source->First(); $unselectedTag = $source->Last(); $value = $source->filter('ID', $selectedTag->ID); // arbitrary subset $field = new TagField('TestField', null, $source, $value); // Not the cleanest way to assert this, but getOptions() is protected $schema = $field->getSchemaDataDefaults(); $this->assertTrue( $this->getFromOptionsByTitle($schema['options'], $selectedTag->Title)['Selected'] ); $this->assertFalse( $this->getFromOptionsByTitle($schema['options'], $unselectedTag->Title)['Selected'] ); } public function testGetSchemaDataDefaults() { $form = new Form(null, 'Form', new FieldList(), new FieldList()); $field = new TagField('TestField', 'Test Field', TagFieldTestBlogTag::get()); $field->setForm($form); $field ->setShouldLazyLoad(false) ->setCanCreate(false); $schema = $field->getSchemaDataDefaults(); $this->assertSame('TestField[]', $schema['name']); $this->assertFalse($schema['lazyLoad']); $this->assertFalse($schema['creatable']); $this->assertEquals([ ['Title' => 'Tag1', 'Value' => 'Tag1', 'Selected' => false], ['Title' => '222', 'Value' => '222', 'Selected' => false], ], $schema['options']); $field->setValue(['222']); $schema = $field->getSchemaDataDefaults(); $this->assertEquals([ ['Title' => 'Tag1', 'Value' => 'Tag1', 'Selected' => false], ['Title' => '222', 'Value' => '222', 'Selected' => true], ], $schema['options']); $field ->setShouldLazyLoad(true) ->setCanCreate(true); $schema = $field->getSchemaDataDefaults(); $this->assertTrue($schema['lazyLoad']); $this->assertTrue($schema['creatable']); $this->assertStringContainsString('suggest', $schema['optionUrl']); } public function testSchemaIsAddedToAttributes() { $field = new TagField('TestField'); $attributes = $field->getAttributes(); $this->assertNotEmpty($attributes['data-schema']); } /** * @param array $options * @param string $title * @return array|null */ protected function getFromOptionsByTitle(array $options, $title) { foreach ($options as $option) { if ($option['Title'] == $title) { return $option; } } return null; } } |