2015-05-15 12:29:17 +02:00
|
|
|
<?php
|
|
|
|
|
2017-06-22 07:15:17 +02:00
|
|
|
namespace SilverStripe\TagField\Tests;
|
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2018-11-16 10:29:49 +01:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\TagField\StringTagField;
|
2017-06-22 07:15:17 +02:00
|
|
|
use SilverStripe\TagField\Tests\Stub\StringTagFieldTestBlogPost;
|
2022-11-24 05:39:57 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2017-01-13 20:11:59 +01:00
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
class StringTagFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-06-22 07:15:17 +02:00
|
|
|
protected static $fixture_file = 'StringTagFieldTest.yml';
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-11-16 10:29:49 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2017-06-22 07:15:17 +02:00
|
|
|
StringTagFieldTestBlogPost::class,
|
2018-11-16 10:29:49 +01:00
|
|
|
];
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
public function testItSavesTagsOnNewRecords()
|
|
|
|
{
|
|
|
|
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
|
|
|
|
|
|
|
$field = new StringTagField('Tags');
|
2018-11-16 10:29:49 +01:00
|
|
|
$field->setValue(['Tag1', 'Tag2']);
|
2015-11-18 05:05:38 +01:00
|
|
|
$field->saveInto($record);
|
|
|
|
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
$this->assertEquals('Tag1,Tag2', $record->Tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return StringTagFieldTestBlogPost
|
|
|
|
*/
|
|
|
|
protected function getNewStringTagFieldTestBlogPost($name)
|
|
|
|
{
|
|
|
|
return $this->objFromFixture(
|
2017-06-22 07:15:17 +02:00
|
|
|
StringTagFieldTestBlogPost::class,
|
2015-11-18 05:05:38 +01:00
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItSavesTagsOnExistingRecords()
|
|
|
|
{
|
|
|
|
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
$field = new StringTagField('Tags');
|
2018-11-16 10:29:49 +01:00
|
|
|
$field->setValue(['Tag1', 'Tag2']);
|
2015-11-18 05:05:38 +01:00
|
|
|
$field->saveInto($record);
|
|
|
|
|
|
|
|
$this->assertEquals('Tag1,Tag2', $record->Tags);
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:27:49 +02:00
|
|
|
public function testImmediateWriteEnabled()
|
|
|
|
{
|
2022-11-24 05:39:57 +01:00
|
|
|
if (Deprecation::isEnabled()) {
|
|
|
|
$this->markTestSkipped('Test calls deprecated code');
|
|
|
|
}
|
2022-04-04 10:27:49 +02:00
|
|
|
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
|
|
|
$record->write();
|
|
|
|
|
2022-11-24 05:39:57 +01:00
|
|
|
StringTagField::config()->set('immediate_write_enabled', true);
|
2022-04-04 10:27:49 +02:00
|
|
|
|
|
|
|
$field = new StringTagField('Tags');
|
|
|
|
$field->setValue(['Tag1', 'Tag2']);
|
|
|
|
$field->saveInto($record);
|
|
|
|
|
|
|
|
$this->assertEquals('Tag1,Tag2', StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImmediateWriteDisabled()
|
|
|
|
{
|
2022-11-24 05:39:57 +01:00
|
|
|
if (Deprecation::isEnabled()) {
|
|
|
|
$this->markTestSkipped('Test calls deprecated code');
|
|
|
|
}
|
2022-04-04 10:27:49 +02:00
|
|
|
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
|
|
|
|
$record->write();
|
|
|
|
|
2022-11-24 05:39:57 +01:00
|
|
|
StringTagField::config()->set('immediate_write_enabled', false);
|
2022-04-04 10:27:49 +02:00
|
|
|
|
|
|
|
$field = new StringTagField('Tags');
|
|
|
|
$field->setValue(['Tag1', 'Tag2']);
|
|
|
|
$field->saveInto($record);
|
|
|
|
|
|
|
|
$this->assertNull(StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags);
|
|
|
|
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
$this->assertEquals('Tag1,Tag2', StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags);
|
|
|
|
}
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
public function testItSuggestsTags()
|
|
|
|
{
|
2018-11-15 21:59:08 +01:00
|
|
|
$field = new StringTagField('SomeField', 'Some field', ['Tag1', 'Tag2'], []);
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Partial tag title match.
|
|
|
|
*/
|
2018-11-15 21:59:08 +01:00
|
|
|
$request = $this->getNewRequest(['term' => 'Tag']);
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}',
|
|
|
|
$field->suggest($request)->getBody()
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exact tag title match.
|
|
|
|
*/
|
2018-11-15 21:59:08 +01:00
|
|
|
$request = $this->getNewRequest(['term' => 'Tag1']);
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
$this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Case-insensitive tag title match.
|
|
|
|
*/
|
2018-11-15 21:59:08 +01:00
|
|
|
$request = $this->getNewRequest(['term' => 'TAG1']);
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
|
|
|
|
$field->suggest($request)->getBody()
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No tag title match.
|
|
|
|
*/
|
2018-11-15 21:59:08 +01:00
|
|
|
$request = $this->getNewRequest(['term' => 'unknown']);
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'{"items":[]}',
|
|
|
|
$field->suggest($request)->getBody()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-16 10:29:49 +01:00
|
|
|
public function testGetSchemaDataDefaults()
|
|
|
|
{
|
|
|
|
$form = new Form(null, 'Form', new FieldList(), new FieldList());
|
|
|
|
$field = new StringTagField('TestField', 'Test Field', ['one', 'two']);
|
|
|
|
$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' => 'one', 'Value' => 'one'],
|
|
|
|
['Title' => 'two', 'Value' => 'two'],
|
|
|
|
], $schema['options']);
|
|
|
|
|
|
|
|
$field
|
|
|
|
->setShouldLazyLoad(true)
|
|
|
|
->setCanCreate(true);
|
|
|
|
|
|
|
|
$schema = $field->getSchemaDataDefaults();
|
|
|
|
$this->assertTrue($schema['lazyLoad']);
|
|
|
|
$this->assertTrue($schema['creatable']);
|
2021-10-27 07:15:33 +02:00
|
|
|
$this->assertStringContainsString('suggest', $schema['optionUrl']);
|
2018-11-16 10:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchemaIsAddedToAttributes()
|
|
|
|
{
|
|
|
|
$field = new StringTagField('TestField');
|
|
|
|
$attributes = $field->getAttributes();
|
|
|
|
$this->assertNotEmpty($attributes['data-schema']);
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:00:12 +02:00
|
|
|
/**
|
|
|
|
* 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_keys($actualSource->map('ID', 'Title')->toArray());
|
|
|
|
sort($expected);
|
|
|
|
sort($actual);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$actual
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
2017-01-13 20:11:59 +01:00
|
|
|
* @return HTTPRequest
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
|
|
|
protected function getNewRequest(array $parameters)
|
|
|
|
{
|
2017-01-13 20:11:59 +01:00
|
|
|
return new HTTPRequest(
|
2015-11-18 05:05:38 +01:00
|
|
|
'get',
|
|
|
|
'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
|
|
|
|
$parameters
|
|
|
|
);
|
|
|
|
}
|
2015-05-15 12:29:17 +02:00
|
|
|
}
|