|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\TagField\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
use SilverStripe\TagField\StringTagField; |
|
8
|
|
|
use SilverStripe\TagField\Tests\Stub\StringTagFieldTestBlogPost; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @mixin PHPUnit_Framework_TestCase |
|
12
|
|
|
*/ |
|
13
|
|
|
class StringTagFieldTest extends SapphireTest |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $fixture_file = 'StringTagFieldTest.yml'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected static $extra_dataobjects = array( |
|
24
|
|
|
StringTagFieldTestBlogPost::class, |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
public function testItSavesTagsOnNewRecords() |
|
28
|
|
|
{ |
|
29
|
|
|
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); |
|
30
|
|
|
|
|
31
|
|
|
$field = new StringTagField('Tags'); |
|
32
|
|
|
$field->setValue(array('Tag1', 'Tag2')); |
|
33
|
|
|
$field->saveInto($record); |
|
34
|
|
|
|
|
35
|
|
|
$record->write(); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals('Tag1,Tag2', $record->Tags); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* |
|
43
|
|
|
* @return StringTagFieldTestBlogPost |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getNewStringTagFieldTestBlogPost($name) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->objFromFixture( |
|
48
|
|
|
StringTagFieldTestBlogPost::class, |
|
49
|
|
|
$name |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testItSavesTagsOnExistingRecords() |
|
54
|
|
|
{ |
|
55
|
|
|
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); |
|
56
|
|
|
$record->write(); |
|
57
|
|
|
|
|
58
|
|
|
$field = new StringTagField('Tags'); |
|
59
|
|
|
$field->setValue(array('Tag1', 'Tag2')); |
|
60
|
|
|
$field->saveInto($record); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals('Tag1,Tag2', $record->Tags); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testItSuggestsTags() |
|
66
|
|
|
{ |
|
67
|
|
|
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost2'); |
|
68
|
|
|
|
|
69
|
|
|
$field = new StringTagField('Tags'); |
|
70
|
|
|
$field->setRecord($record); |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Partial tag title match. |
|
74
|
|
|
*/ |
|
75
|
|
|
$request = $this->getNewRequest(array('term' => 'Tag')); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals( |
|
78
|
|
|
'{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}', |
|
79
|
|
|
$field->suggest($request)->getBody() |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Exact tag title match. |
|
84
|
|
|
*/ |
|
85
|
|
|
$request = $this->getNewRequest(array('term' => 'Tag1')); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}'); |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Case-insensitive tag title match. |
|
91
|
|
|
*/ |
|
92
|
|
|
$request = $this->getNewRequest(array('term' => 'TAG1')); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals( |
|
95
|
|
|
'{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
96
|
|
|
$field->suggest($request)->getBody() |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* No tag title match. |
|
101
|
|
|
*/ |
|
102
|
|
|
$request = $this->getNewRequest(array('term' => 'unknown')); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertEquals( |
|
105
|
|
|
'{"items":[]}', |
|
106
|
|
|
$field->suggest($request)->getBody() |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param array $parameters |
|
112
|
|
|
* |
|
113
|
|
|
* @return HTTPRequest |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getNewRequest(array $parameters) |
|
116
|
|
|
{ |
|
117
|
|
|
return new HTTPRequest( |
|
118
|
|
|
'get', |
|
119
|
|
|
'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest', |
|
120
|
|
|
$parameters |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|