silverstripe-tagfield/tests/StringTagFieldTest.php
Robbie Averill 518189e2ef SilverStripe 4 compatibility (#87)
* Update composer constraint and branch alias to support SS4 testing

* Add namespaces, update DataList quirk with getSourceList

* Add PSR-4 autoloader definition

* Move template to correct namespace location, update requirement paths

* FIX Visibility on allowed actions

* FIX Update chosen class names to match updates in framework

* Update Travis configuration for 4.x builds. Update docs for namespaced classes.

* Use "4" for the release instead of master.

* FIX Selected tag height. Move Readonly to own class.
2017-01-13 19:11:59 +00:00

170 lines
4.1 KiB
PHP
Executable File

<?php
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\DataObject;
use SilverStripe\TagField\StringTagField;
/**
* @mixin PHPUnit_Framework_TestCase
*/
class StringTagFieldTest extends SapphireTest
{
/**
* @var string
*/
public static $fixture_file = 'tagfield/tests/StringTagFieldTest.yml';
/**
* @var array
*/
protected $extraDataObjects = array(
'StringTagFieldTestBlogPost',
);
public function testItSavesTagsOnNewRecords()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
$field = new StringTagField('Tags');
$field->setValue(array('Tag1', 'Tag2'));
$field->saveInto($record);
$record->write();
$this->assertEquals('Tag1,Tag2', $record->Tags);
}
/**
* @param string $name
*
* @return StringTagFieldTestBlogPost
*/
protected function getNewStringTagFieldTestBlogPost($name)
{
return $this->objFromFixture(
'StringTagFieldTestBlogPost',
$name
);
}
public function testItSavesTagsOnExistingRecords()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
$record->write();
$field = new StringTagField('Tags');
$field->setValue(array('Tag1', 'Tag2'));
$field->saveInto($record);
$this->assertEquals('Tag1,Tag2', $record->Tags);
}
public function testItSuggestsTags()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost2');
$field = new StringTagField('Tags');
$field->setRecord($record);
/**
* Partial tag title match.
*/
$request = $this->getNewRequest(array('term' => 'Tag'));
$this->assertEquals(
'{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}',
$field->suggest($request)->getBody()
);
/**
* Exact tag title match.
*/
$request = $this->getNewRequest(array('term' => 'Tag1'));
$this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}');
/**
* Case-insensitive tag title match.
*/
$request = $this->getNewRequest(array('term' => 'TAG1'));
$this->assertEquals(
'{"items":[{"id":"Tag1","text":"Tag1"}]}',
$field->suggest($request)->getBody()
);
/**
* No tag title match.
*/
$request = $this->getNewRequest(array('term' => 'unknown'));
$this->assertEquals(
'{"items":[]}',
$field->suggest($request)->getBody()
);
}
/**
* @param array $parameters
*
* @return HTTPRequest
*/
protected function getNewRequest(array $parameters)
{
return new HTTPRequest(
'get',
'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
$parameters
);
}
}
/**
* @property string $Tags
*/
class StringTagFieldTestBlogPost extends DataObject implements TestOnly
{
/**
* @var array
*/
private static $db = array(
'Title' => 'Text',
'Content' => 'Text',
'Tags' => 'Text',
);
}
class StringTagFieldTestController extends Controller implements TestOnly
{
/**
* @return Form
*/
public function StringTagFieldTestForm()
{
$fields = new FieldList(
$tagField = new StringTagField('Tags')
);
$actions = new FieldList(
new FormAction('StringTagFieldTestFormSubmit')
);
return new Form($this, 'StringTagFieldTestForm', $fields, $actions);
}
/**
* @param DataObject $dataObject
* @param Form $form
*/
public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form)
{
$form->saveInto($dataObject);
}
}