silverstripe-tagfield/tests/StringTagFieldTest.php

170 lines
4.1 KiB
PHP
Raw Normal View History

2015-05-15 12:29:17 +02:00
<?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;
2015-05-15 12:29:17 +02:00
/**
* @mixin PHPUnit_Framework_TestCase
*/
2015-11-18 05:05:38 +01:00
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
2015-11-18 05:05:38 +01:00
*/
protected function getNewRequest(array $parameters)
{
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
}
/**
* @property string $Tags
*/
2015-11-18 05:05:38 +01:00
class StringTagFieldTestBlogPost extends DataObject implements TestOnly
{
/**
* @var array
*/
private static $db = array(
'Title' => 'Text',
'Content' => 'Text',
'Tags' => 'Text',
);
2015-05-15 12:29:17 +02:00
}
2015-11-18 05:05:38 +01:00
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);
}
2015-05-15 12:29:17 +02:00
}