@@ -5,329 +5,329 @@ discard block |
||
| 5 | 5 | */ |
| 6 | 6 | class TagFieldTest extends SapphireTest |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * @var string |
|
| 10 | - */ |
|
| 11 | - public static $fixture_file = 'tagfield/tests/TagFieldTest.yml'; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - protected $extraDataObjects = array( |
|
| 17 | - 'TagFieldTestBlogTag', |
|
| 18 | - 'TagFieldTestBlogPost', |
|
| 19 | - ); |
|
| 20 | - |
|
| 21 | - public function testItSavesLinksToNewTagsOnNewRecords() |
|
| 22 | - { |
|
| 23 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 24 | - |
|
| 25 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 26 | - $field->setValue(array('Tag3', 'Tag4')); |
|
| 27 | - $field->saveInto($record); |
|
| 28 | - |
|
| 29 | - $record->write(); |
|
| 30 | - |
|
| 31 | - $this->compareExpectedAndActualTags( |
|
| 32 | - array('Tag3', 'Tag4'), |
|
| 33 | - $record |
|
| 34 | - ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param string $name |
|
| 39 | - * |
|
| 40 | - * @return TagFieldTestBlogPost |
|
| 41 | - */ |
|
| 42 | - protected function getNewTagFieldTestBlogPost($name) |
|
| 43 | - { |
|
| 44 | - return $this->objFromFixture( |
|
| 45 | - 'TagFieldTestBlogPost', |
|
| 46 | - $name |
|
| 47 | - ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param array $expected |
|
| 52 | - * @param TagFieldTestBlogPost $record |
|
| 53 | - */ |
|
| 54 | - protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) |
|
| 55 | - { |
|
| 56 | - $this->compareTagLists($expected, $record->Tags()); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Ensure a source of tags matches the given string tag names |
|
| 61 | - * |
|
| 62 | - * @param array $expected |
|
| 63 | - * @param DataList $actualSource |
|
| 64 | - */ |
|
| 65 | - protected function compareTagLists(array $expected, DataList $actualSource) |
|
| 66 | - { |
|
| 67 | - $actual = array_values($actualSource->map('ID', 'Title')->toArray()); |
|
| 68 | - |
|
| 69 | - sort($expected); |
|
| 70 | - sort($actual); |
|
| 71 | - |
|
| 72 | - $this->assertEquals( |
|
| 73 | - $expected, |
|
| 74 | - $actual |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - public function testItSavesLinksToNewTagsOnExistingRecords() |
|
| 79 | - { |
|
| 80 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 81 | - $record->write(); |
|
| 82 | - |
|
| 83 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 84 | - $field->setValue(array('Tag3', 'Tag4')); |
|
| 85 | - $field->saveInto($record); |
|
| 86 | - |
|
| 87 | - $this->compareExpectedAndActualTags( |
|
| 88 | - array('Tag3', 'Tag4'), |
|
| 89 | - $record |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function testItSavesLinksToExistingTagsOnNewRecords() |
|
| 94 | - { |
|
| 95 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 96 | - |
|
| 97 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 98 | - $field->setValue(array('Tag1', 'Tag2')); |
|
| 99 | - $field->saveInto($record); |
|
| 100 | - |
|
| 101 | - $record->write(); |
|
| 102 | - |
|
| 103 | - $this->compareExpectedAndActualTags( |
|
| 104 | - array('Tag1', 'Tag2'), |
|
| 105 | - $record |
|
| 106 | - ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function testItSavesLinksToExistingTagsOnExistingRecords() |
|
| 110 | - { |
|
| 111 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 112 | - $record->write(); |
|
| 113 | - |
|
| 114 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 115 | - $field->setValue(array('Tag1', 'Tag2')); |
|
| 116 | - $field->saveInto($record); |
|
| 117 | - |
|
| 118 | - $this->compareExpectedAndActualTags( |
|
| 119 | - array('Tag1', 'Tag2'), |
|
| 120 | - $record |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Ensure that {@see TagField::saveInto} respects existing tags |
|
| 126 | - */ |
|
| 127 | - public function testSaveDuplicateTags() |
|
| 128 | - { |
|
| 129 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost2'); |
|
| 130 | - $record->write(); |
|
| 131 | - $tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2'); |
|
| 132 | - |
|
| 133 | - // Check tags before write |
|
| 134 | - $this->compareExpectedAndActualTags( |
|
| 135 | - array('Tag1', 'Tag2'), |
|
| 136 | - $record |
|
| 137 | - ); |
|
| 138 | - $this->compareTagLists( |
|
| 139 | - array('Tag1', 'Tag2'), |
|
| 140 | - TagFieldTestBlogTag::get() |
|
| 141 | - ); |
|
| 142 | - $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 143 | - |
|
| 144 | - // Write new tags |
|
| 145 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 146 | - $field->setValue(array('Tag2', 'Tag3')); |
|
| 147 | - $field->saveInto($record); |
|
| 148 | - |
|
| 149 | - // Check only one new tag was added |
|
| 150 | - $this->compareExpectedAndActualTags( |
|
| 151 | - array('Tag2', 'Tag3'), |
|
| 152 | - $record |
|
| 153 | - ); |
|
| 154 | - |
|
| 155 | - // Ensure that only one new dataobject was added and that tag2s id has not changed |
|
| 156 | - $this->compareTagLists( |
|
| 157 | - array('Tag1', 'Tag2', 'Tag3'), |
|
| 158 | - TagFieldTestBlogTag::get() |
|
| 159 | - ); |
|
| 160 | - $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - public function testItSuggestsTags() |
|
| 164 | - { |
|
| 165 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Partial tag title match. |
|
| 169 | - */ |
|
| 170 | - $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 171 | - |
|
| 172 | - $tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1'); |
|
| 173 | - $tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2'); |
|
| 174 | - |
|
| 175 | - $this->assertEquals( |
|
| 176 | - sprintf('{"items":[{"id":%d,"text":"Tag1"},{"id":%d,"text":"Tag2"}]}', $tag1ID, $tag2ID), |
|
| 177 | - $field->suggest($request)->getBody() |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Exact tag title match. |
|
| 182 | - */ |
|
| 183 | - $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
| 184 | - |
|
| 185 | - $this->assertEquals( |
|
| 186 | - sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 187 | - $field->suggest($request)->getBody() |
|
| 188 | - ); |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Case-insensitive tag title match. |
|
| 192 | - */ |
|
| 193 | - $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
| 194 | - |
|
| 195 | - $this->assertEquals( |
|
| 196 | - sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 197 | - $field->suggest($request)->getBody() |
|
| 198 | - ); |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * No tag title match. |
|
| 202 | - */ |
|
| 203 | - $request = $this->getNewRequest(array('term' => 'unknown')); |
|
| 204 | - |
|
| 205 | - $this->assertEquals( |
|
| 206 | - '{"items":[]}', |
|
| 207 | - $field->suggest($request)->getBody() |
|
| 208 | - ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Tests that TagField supports pre-filtered data sources |
|
| 213 | - */ |
|
| 214 | - public function testRestrictedSuggestions() |
|
| 215 | - { |
|
| 216 | - $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); |
|
| 217 | - $field = new TagField('Tags', '', $source); |
|
| 218 | - $tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1'); |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Partial tag title match. |
|
| 222 | - */ |
|
| 223 | - $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 224 | - |
|
| 225 | - $this->assertEquals( |
|
| 226 | - sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 227 | - $field->suggest($request)->getBody() |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Exact tag title match. |
|
| 232 | - */ |
|
| 233 | - $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
| 234 | - |
|
| 235 | - $this->assertEquals( |
|
| 236 | - sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 237 | - $field->suggest($request)->getBody() |
|
| 238 | - ); |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Excluded item doesn't appear in matches |
|
| 242 | - */ |
|
| 243 | - $request = $this->getNewRequest(array('term' => 'Tag2')); |
|
| 244 | - |
|
| 245 | - $this->assertEquals( |
|
| 246 | - '{"items":[]}', |
|
| 247 | - $field->suggest($request)->getBody() |
|
| 248 | - ); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * @param array $parameters |
|
| 253 | - * |
|
| 254 | - * @return SS_HTTPRequest |
|
| 255 | - */ |
|
| 256 | - protected function getNewRequest(array $parameters) |
|
| 257 | - { |
|
| 258 | - return new SS_HTTPRequest( |
|
| 259 | - 'get', |
|
| 260 | - 'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest', |
|
| 261 | - $parameters |
|
| 262 | - ); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - public function testItDisplaysValuesFromRelations() |
|
| 266 | - { |
|
| 267 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 268 | - $record->write(); |
|
| 269 | - |
|
| 270 | - $form = new Form( |
|
| 271 | - new TagFieldTestController($record), |
|
| 272 | - 'Form', |
|
| 273 | - new FieldList( |
|
| 274 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')) |
|
| 275 | - ), |
|
| 276 | - new FieldList() |
|
| 277 | - ); |
|
| 278 | - |
|
| 279 | - $form->loadDataFrom( |
|
| 280 | - $this->objFromFixture('TagFieldTestBlogPost', 'BlogPost2') |
|
| 281 | - ); |
|
| 282 | - |
|
| 283 | - $ids = TagFieldTestBlogTag::get()->map('ID', 'ID')->toArray(); |
|
| 284 | - |
|
| 285 | - $this->assertEquals($field->Value(), $ids); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - public function testItIgnoresNewTagsIfCannotCreate() |
|
| 289 | - { |
|
| 290 | - $record = new TagFieldTestBlogPost(); |
|
| 291 | - $record->write(); |
|
| 292 | - |
|
| 293 | - $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first(); |
|
| 294 | - |
|
| 295 | - $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'), array($tag->ID, 'Tag3')); |
|
| 296 | - $field->setCanCreate(false); |
|
| 297 | - $field->saveInto($record); |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @var TagFieldTestBlogPost $record |
|
| 301 | - */ |
|
| 302 | - $record = DataObject::get_by_id('TagFieldTestBlogPost', $record->ID); |
|
| 303 | - |
|
| 304 | - $this->compareExpectedAndActualTags( |
|
| 305 | - array('Tag1'), |
|
| 306 | - $record |
|
| 307 | - ); |
|
| 308 | - } |
|
| 8 | + /** |
|
| 9 | + * @var string |
|
| 10 | + */ |
|
| 11 | + public static $fixture_file = 'tagfield/tests/TagFieldTest.yml'; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + protected $extraDataObjects = array( |
|
| 17 | + 'TagFieldTestBlogTag', |
|
| 18 | + 'TagFieldTestBlogPost', |
|
| 19 | + ); |
|
| 20 | + |
|
| 21 | + public function testItSavesLinksToNewTagsOnNewRecords() |
|
| 22 | + { |
|
| 23 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 24 | + |
|
| 25 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 26 | + $field->setValue(array('Tag3', 'Tag4')); |
|
| 27 | + $field->saveInto($record); |
|
| 28 | + |
|
| 29 | + $record->write(); |
|
| 30 | + |
|
| 31 | + $this->compareExpectedAndActualTags( |
|
| 32 | + array('Tag3', 'Tag4'), |
|
| 33 | + $record |
|
| 34 | + ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param string $name |
|
| 39 | + * |
|
| 40 | + * @return TagFieldTestBlogPost |
|
| 41 | + */ |
|
| 42 | + protected function getNewTagFieldTestBlogPost($name) |
|
| 43 | + { |
|
| 44 | + return $this->objFromFixture( |
|
| 45 | + 'TagFieldTestBlogPost', |
|
| 46 | + $name |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param array $expected |
|
| 52 | + * @param TagFieldTestBlogPost $record |
|
| 53 | + */ |
|
| 54 | + protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) |
|
| 55 | + { |
|
| 56 | + $this->compareTagLists($expected, $record->Tags()); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Ensure a source of tags matches the given string tag names |
|
| 61 | + * |
|
| 62 | + * @param array $expected |
|
| 63 | + * @param DataList $actualSource |
|
| 64 | + */ |
|
| 65 | + protected function compareTagLists(array $expected, DataList $actualSource) |
|
| 66 | + { |
|
| 67 | + $actual = array_values($actualSource->map('ID', 'Title')->toArray()); |
|
| 68 | + |
|
| 69 | + sort($expected); |
|
| 70 | + sort($actual); |
|
| 71 | + |
|
| 72 | + $this->assertEquals( |
|
| 73 | + $expected, |
|
| 74 | + $actual |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + public function testItSavesLinksToNewTagsOnExistingRecords() |
|
| 79 | + { |
|
| 80 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 81 | + $record->write(); |
|
| 82 | + |
|
| 83 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 84 | + $field->setValue(array('Tag3', 'Tag4')); |
|
| 85 | + $field->saveInto($record); |
|
| 86 | + |
|
| 87 | + $this->compareExpectedAndActualTags( |
|
| 88 | + array('Tag3', 'Tag4'), |
|
| 89 | + $record |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function testItSavesLinksToExistingTagsOnNewRecords() |
|
| 94 | + { |
|
| 95 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 96 | + |
|
| 97 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 98 | + $field->setValue(array('Tag1', 'Tag2')); |
|
| 99 | + $field->saveInto($record); |
|
| 100 | + |
|
| 101 | + $record->write(); |
|
| 102 | + |
|
| 103 | + $this->compareExpectedAndActualTags( |
|
| 104 | + array('Tag1', 'Tag2'), |
|
| 105 | + $record |
|
| 106 | + ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function testItSavesLinksToExistingTagsOnExistingRecords() |
|
| 110 | + { |
|
| 111 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 112 | + $record->write(); |
|
| 113 | + |
|
| 114 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 115 | + $field->setValue(array('Tag1', 'Tag2')); |
|
| 116 | + $field->saveInto($record); |
|
| 117 | + |
|
| 118 | + $this->compareExpectedAndActualTags( |
|
| 119 | + array('Tag1', 'Tag2'), |
|
| 120 | + $record |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Ensure that {@see TagField::saveInto} respects existing tags |
|
| 126 | + */ |
|
| 127 | + public function testSaveDuplicateTags() |
|
| 128 | + { |
|
| 129 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost2'); |
|
| 130 | + $record->write(); |
|
| 131 | + $tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2'); |
|
| 132 | + |
|
| 133 | + // Check tags before write |
|
| 134 | + $this->compareExpectedAndActualTags( |
|
| 135 | + array('Tag1', 'Tag2'), |
|
| 136 | + $record |
|
| 137 | + ); |
|
| 138 | + $this->compareTagLists( |
|
| 139 | + array('Tag1', 'Tag2'), |
|
| 140 | + TagFieldTestBlogTag::get() |
|
| 141 | + ); |
|
| 142 | + $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 143 | + |
|
| 144 | + // Write new tags |
|
| 145 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 146 | + $field->setValue(array('Tag2', 'Tag3')); |
|
| 147 | + $field->saveInto($record); |
|
| 148 | + |
|
| 149 | + // Check only one new tag was added |
|
| 150 | + $this->compareExpectedAndActualTags( |
|
| 151 | + array('Tag2', 'Tag3'), |
|
| 152 | + $record |
|
| 153 | + ); |
|
| 154 | + |
|
| 155 | + // Ensure that only one new dataobject was added and that tag2s id has not changed |
|
| 156 | + $this->compareTagLists( |
|
| 157 | + array('Tag1', 'Tag2', 'Tag3'), |
|
| 158 | + TagFieldTestBlogTag::get() |
|
| 159 | + ); |
|
| 160 | + $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + public function testItSuggestsTags() |
|
| 164 | + { |
|
| 165 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')); |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Partial tag title match. |
|
| 169 | + */ |
|
| 170 | + $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 171 | + |
|
| 172 | + $tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1'); |
|
| 173 | + $tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2'); |
|
| 174 | + |
|
| 175 | + $this->assertEquals( |
|
| 176 | + sprintf('{"items":[{"id":%d,"text":"Tag1"},{"id":%d,"text":"Tag2"}]}', $tag1ID, $tag2ID), |
|
| 177 | + $field->suggest($request)->getBody() |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Exact tag title match. |
|
| 182 | + */ |
|
| 183 | + $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
| 184 | + |
|
| 185 | + $this->assertEquals( |
|
| 186 | + sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 187 | + $field->suggest($request)->getBody() |
|
| 188 | + ); |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Case-insensitive tag title match. |
|
| 192 | + */ |
|
| 193 | + $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
| 194 | + |
|
| 195 | + $this->assertEquals( |
|
| 196 | + sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 197 | + $field->suggest($request)->getBody() |
|
| 198 | + ); |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * No tag title match. |
|
| 202 | + */ |
|
| 203 | + $request = $this->getNewRequest(array('term' => 'unknown')); |
|
| 204 | + |
|
| 205 | + $this->assertEquals( |
|
| 206 | + '{"items":[]}', |
|
| 207 | + $field->suggest($request)->getBody() |
|
| 208 | + ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Tests that TagField supports pre-filtered data sources |
|
| 213 | + */ |
|
| 214 | + public function testRestrictedSuggestions() |
|
| 215 | + { |
|
| 216 | + $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); |
|
| 217 | + $field = new TagField('Tags', '', $source); |
|
| 218 | + $tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1'); |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Partial tag title match. |
|
| 222 | + */ |
|
| 223 | + $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 224 | + |
|
| 225 | + $this->assertEquals( |
|
| 226 | + sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 227 | + $field->suggest($request)->getBody() |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Exact tag title match. |
|
| 232 | + */ |
|
| 233 | + $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
| 234 | + |
|
| 235 | + $this->assertEquals( |
|
| 236 | + sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID), |
|
| 237 | + $field->suggest($request)->getBody() |
|
| 238 | + ); |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Excluded item doesn't appear in matches |
|
| 242 | + */ |
|
| 243 | + $request = $this->getNewRequest(array('term' => 'Tag2')); |
|
| 244 | + |
|
| 245 | + $this->assertEquals( |
|
| 246 | + '{"items":[]}', |
|
| 247 | + $field->suggest($request)->getBody() |
|
| 248 | + ); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * @param array $parameters |
|
| 253 | + * |
|
| 254 | + * @return SS_HTTPRequest |
|
| 255 | + */ |
|
| 256 | + protected function getNewRequest(array $parameters) |
|
| 257 | + { |
|
| 258 | + return new SS_HTTPRequest( |
|
| 259 | + 'get', |
|
| 260 | + 'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest', |
|
| 261 | + $parameters |
|
| 262 | + ); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + public function testItDisplaysValuesFromRelations() |
|
| 266 | + { |
|
| 267 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 268 | + $record->write(); |
|
| 269 | + |
|
| 270 | + $form = new Form( |
|
| 271 | + new TagFieldTestController($record), |
|
| 272 | + 'Form', |
|
| 273 | + new FieldList( |
|
| 274 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')) |
|
| 275 | + ), |
|
| 276 | + new FieldList() |
|
| 277 | + ); |
|
| 278 | + |
|
| 279 | + $form->loadDataFrom( |
|
| 280 | + $this->objFromFixture('TagFieldTestBlogPost', 'BlogPost2') |
|
| 281 | + ); |
|
| 282 | + |
|
| 283 | + $ids = TagFieldTestBlogTag::get()->map('ID', 'ID')->toArray(); |
|
| 284 | + |
|
| 285 | + $this->assertEquals($field->Value(), $ids); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + public function testItIgnoresNewTagsIfCannotCreate() |
|
| 289 | + { |
|
| 290 | + $record = new TagFieldTestBlogPost(); |
|
| 291 | + $record->write(); |
|
| 292 | + |
|
| 293 | + $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first(); |
|
| 294 | + |
|
| 295 | + $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'), array($tag->ID, 'Tag3')); |
|
| 296 | + $field->setCanCreate(false); |
|
| 297 | + $field->saveInto($record); |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @var TagFieldTestBlogPost $record |
|
| 301 | + */ |
|
| 302 | + $record = DataObject::get_by_id('TagFieldTestBlogPost', $record->ID); |
|
| 303 | + |
|
| 304 | + $this->compareExpectedAndActualTags( |
|
| 305 | + array('Tag1'), |
|
| 306 | + $record |
|
| 307 | + ); |
|
| 308 | + } |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | class TagFieldTestBlogTag extends DataObject implements TestOnly |
| 312 | 312 | { |
| 313 | - /** |
|
| 314 | - * @var string |
|
| 315 | - */ |
|
| 316 | - private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC'; |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @var array |
|
| 320 | - */ |
|
| 321 | - private static $db = array( |
|
| 322 | - 'Title' => 'Varchar(200)', |
|
| 323 | - ); |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * @var array |
|
| 327 | - */ |
|
| 328 | - private static $belongs_many_many = array( |
|
| 329 | - 'BlogPosts' => 'TagFieldTestBlogPost', |
|
| 330 | - ); |
|
| 313 | + /** |
|
| 314 | + * @var string |
|
| 315 | + */ |
|
| 316 | + private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC'; |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @var array |
|
| 320 | + */ |
|
| 321 | + private static $db = array( |
|
| 322 | + 'Title' => 'Varchar(200)', |
|
| 323 | + ); |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * @var array |
|
| 327 | + */ |
|
| 328 | + private static $belongs_many_many = array( |
|
| 329 | + 'BlogPosts' => 'TagFieldTestBlogPost', |
|
| 330 | + ); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | /** |
@@ -335,46 +335,46 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | class TagFieldTestBlogPost extends DataObject implements TestOnly |
| 337 | 337 | { |
| 338 | - /** |
|
| 339 | - * @var array |
|
| 340 | - */ |
|
| 341 | - private static $db = array( |
|
| 342 | - 'Title' => 'Text', |
|
| 343 | - 'Content' => 'Text', |
|
| 344 | - ); |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * @var array |
|
| 348 | - */ |
|
| 349 | - private static $many_many = array( |
|
| 350 | - 'Tags' => 'TagFieldTestBlogTag', |
|
| 351 | - ); |
|
| 338 | + /** |
|
| 339 | + * @var array |
|
| 340 | + */ |
|
| 341 | + private static $db = array( |
|
| 342 | + 'Title' => 'Text', |
|
| 343 | + 'Content' => 'Text', |
|
| 344 | + ); |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * @var array |
|
| 348 | + */ |
|
| 349 | + private static $many_many = array( |
|
| 350 | + 'Tags' => 'TagFieldTestBlogTag', |
|
| 351 | + ); |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | class TagFieldTestController extends Controller implements TestOnly |
| 355 | 355 | { |
| 356 | - /** |
|
| 357 | - * @return Form |
|
| 358 | - */ |
|
| 359 | - public function TagFieldTestForm() |
|
| 360 | - { |
|
| 361 | - $fields = new FieldList( |
|
| 362 | - $tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')) |
|
| 363 | - ); |
|
| 364 | - |
|
| 365 | - $actions = new FieldList( |
|
| 366 | - new FormAction('TagFieldTestFormSubmit') |
|
| 367 | - ); |
|
| 368 | - |
|
| 369 | - return new Form($this, 'TagFieldTestForm', $fields, $actions); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * @param DataObject $dataObject |
|
| 374 | - * @param Form $form |
|
| 375 | - */ |
|
| 376 | - public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form) |
|
| 377 | - { |
|
| 378 | - $form->saveInto($dataObject); |
|
| 379 | - } |
|
| 356 | + /** |
|
| 357 | + * @return Form |
|
| 358 | + */ |
|
| 359 | + public function TagFieldTestForm() |
|
| 360 | + { |
|
| 361 | + $fields = new FieldList( |
|
| 362 | + $tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')) |
|
| 363 | + ); |
|
| 364 | + |
|
| 365 | + $actions = new FieldList( |
|
| 366 | + new FormAction('TagFieldTestFormSubmit') |
|
| 367 | + ); |
|
| 368 | + |
|
| 369 | + return new Form($this, 'TagFieldTestForm', $fields, $actions); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * @param DataObject $dataObject |
|
| 374 | + * @param Form $form |
|
| 375 | + */ |
|
| 376 | + public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form) |
|
| 377 | + { |
|
| 378 | + $form->saveInto($dataObject); |
|
| 379 | + } |
|
| 380 | 380 | } |