forked from ezsystems/ezpublish-api
/
KeywordIntegrationTest.php
530 lines (473 loc) · 15.2 KB
/
KeywordIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
/**
* File contains: eZ\Publish\API\Repository\Tests\FieldType\KeywordIntegrationTest class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\API\Repository\Tests\FieldType;
use eZ\Publish\Core\FieldType\Keyword\Value as KeywordValue;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\Content\Field;
/**
* Integration test for use field type.
*
* @group integration
* @group field-type
*/
class KeywordIntegrationTest extends SearchMultivaluedBaseIntegrationTest
{
/**
* Get name of tested field type.
*
* @return string
*/
public function getTypeName()
{
return 'ezkeyword';
}
/**
* Get expected settings schema.
*
* @return array
*/
public function getSettingsSchema()
{
return array();
}
/**
* Get a valid $fieldSettings value.
*
* @return mixed
*/
public function getValidFieldSettings()
{
return array();
}
/**
* Get $fieldSettings value not accepted by the field type.
*
* @return mixed
*/
public function getInvalidFieldSettings()
{
return array(
'somethingUnknown' => 0,
);
}
/**
* Get expected validator schema.
*
* @return array
*/
public function getValidatorSchema()
{
return array();
}
/**
* Get a valid $validatorConfiguration.
*
* @return mixed
*/
public function getValidValidatorConfiguration()
{
return array();
}
/**
* Get $validatorConfiguration not accepted by the field type.
*
* @return mixed
*/
public function getInvalidValidatorConfiguration()
{
return array(
'unknown' => array('value' => 23),
);
}
/**
* Get initial field data for valid object creation.
*
* @return mixed
*/
public function getValidCreationFieldData()
{
return new KeywordValue(array('foo', 'bar', 'sindelfingen'));
}
/**
* {@inheritdoc}
*/
public function getValidMultilingualFieldData(array $languageCodes)
{
$data = [];
foreach ($languageCodes as $languageCode) {
$data[$languageCode] = new KeywordValue(
[
"{$languageCode} bar",
"{$languageCode} foo",
"$languageCode sindelfingen",
]
);
}
return $data;
}
/**
* Get name generated by the given field type (either via Nameable or fieldType->getName()).
*
* @return string
*/
public function getFieldName()
{
return 'foo, bar, sindelfingen';
}
/**
* Asserts that the field data was loaded correctly.
*
* Asserts that the data provided by {@link getValidCreationFieldData()}
* was stored and loaded correctly.
*
* @param Field $field
*/
public function assertFieldDataLoadedCorrect(Field $field)
{
$this->assertInstanceOf(
'eZ\\Publish\\Core\\FieldType\\Keyword\\Value',
$field->value
);
$this->assertEquals(
array('foo' => true, 'bar' => true, 'sindelfingen' => true),
array_fill_keys($field->value->values, true)
);
}
/**
* Get field data which will result in errors during creation.
*
* This is a PHPUnit data provider.
*
* The returned records must contain of an error producing data value and
* the expected exception class (from the API or SPI, not implementation
* specific!) as the second element. For example:
*
* <code>
* array(
* array(
* new DoomedValue( true ),
* 'eZ\\Publish\\API\\Repository\\Exceptions\\ContentValidationException'
* ),
* // ...
* );
* </code>
*
* @return array[]
*/
public function provideInvalidCreationFieldData()
{
return array(
array(
new \stdClass(),
'eZ\\Publish\\Core\\Base\\Exceptions\\InvalidArgumentType',
),
array(
23,
'eZ\\Publish\\Core\\Base\\Exceptions\\InvalidArgumentType',
),
);
}
/**
* Get update field externals data.
*
* @return array
*/
public function getValidUpdateFieldData()
{
return new KeywordValue(array('bielefeld'));
}
/**
* Get externals updated field data values.
*
* This is a PHPUnit data provider
*
* @return array
*/
public function assertUpdatedFieldDataLoadedCorrect(Field $field)
{
$this->assertInstanceOf(
'eZ\\Publish\\Core\\FieldType\\Keyword\\Value',
$field->value
);
$this->assertEquals(
array('bielefeld' => true),
array_fill_keys($field->value->values, true)
);
}
/**
* Get field data which will result in errors during update.
*
* This is a PHPUnit data provider.
*
* The returned records must contain of an error producing data value and
* the expected exception class (from the API or SPI, not implementation
* specific!) as the second element. For example:
*
* <code>
* array(
* array(
* new DoomedValue( true ),
* 'eZ\\Publish\\API\\Repository\\Exceptions\\ContentValidationException'
* ),
* // ...
* );
* </code>
*
* @return array[]
*/
public function provideInvalidUpdateFieldData()
{
return $this->provideInvalidCreationFieldData();
}
/**
* Asserts the the field data was loaded correctly.
*
* Asserts that the data provided by {@link getValidCreationFieldData()}
* was copied and loaded correctly.
*
* @param Field $field
*/
public function assertCopiedFieldDataLoadedCorrectly(Field $field)
{
$this->assertInstanceOf(
'eZ\\Publish\\Core\\FieldType\\Keyword\\Value',
$field->value
);
$this->assertEquals(
array('foo' => true, 'bar' => true, 'sindelfingen' => true),
array_fill_keys($field->value->values, true)
);
}
/**
* Get data to test to hash method.
*
* This is a PHPUnit data provider
*
* The returned records must have the the original value assigned to the
* first index and the expected hash result to the second. For example:
*
* <code>
* array(
* array(
* new MyValue( true ),
* array( 'myValue' => true ),
* ),
* // ...
* );
* </code>
*
* @return array
*/
public function provideToHashData()
{
return array(
array(
new KeywordValue(array('bielefeld', 'sindelfingen')),
array('bielefeld', 'sindelfingen'),
),
);
}
/**
* Get expectations for the fromHash call on our field value.
*
* This is a PHPUnit data provider
*
* @return array
*/
public function provideFromHashData()
{
return array(
array(
array('sindelfeld', 'bielefingen'),
new KeywordValue(array('sindelfeld', 'bielefingen')),
),
);
}
public function providerForTestIsEmptyValue()
{
return array(
array(new KeywordValue()),
array(new KeywordValue(null)),
array(new KeywordValue(array())),
);
}
public function providerForTestIsNotEmptyValue()
{
return array(
array(
$this->getValidCreationFieldData(),
),
array(
new KeywordValue(array('0')),
),
);
}
/**
* Test updating multiple contents with ezkeyword field preserves proper fields values.
*/
public function testUpdateContentKeywords()
{
$contentType = $this->testCreateContentType();
$contentService = $this->getRepository()->getContentService();
$value01 = new KeywordValue(['foo', 'FOO', 'bar', 'baz']);
$contentDraft = $this->createContent($value01, $contentType);
$publishedContent01 = $contentService->publishVersion($contentDraft->versionInfo);
$this->assertContentFieldHasCorrectData($publishedContent01->contentInfo->id, $value01);
// create another content with the same value
$value02 = $value01;
$contentDraft = $this->createContent($value02, $contentType);
$publishedContent02 = $contentService->publishVersion($contentDraft->versionInfo);
$this->assertContentFieldHasCorrectData($publishedContent02->contentInfo->id, $value02);
// for the first content, create draft, remove one keyword and publish new version
$contentDraft = $contentService->createContentDraft($publishedContent01->contentInfo);
$updateStruct = $contentService->newContentUpdateStruct();
$value01 = new KeywordValue(['foo', 'FOO', 'bar']);
$updateStruct->setField('data', $value01);
$contentDraft = $contentService->updateContent($contentDraft->versionInfo, $updateStruct);
$publishedContent01 = $contentService->publishVersion($contentDraft->versionInfo);
$this->assertContentFieldHasCorrectData($publishedContent01->contentInfo->id, $value01);
// reload and check the second content value01
$this->assertContentFieldHasCorrectData($publishedContent02->contentInfo->id, $value02);
// delete the second content
$contentService->deleteContent($publishedContent02->contentInfo);
// check if the first content was not affected
$this->assertContentFieldHasCorrectData($publishedContent01->contentInfo->id, $value01);
}
/**
* {@inheritdoc}
*/
protected function createContent($fieldData, $contentType = null)
{
if ($contentType === null) {
$contentType = $this->testCreateContentType();
}
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$createStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
$createStruct->setField('name', 'Test object');
$createStruct->setField(
'data',
$fieldData
);
$createStruct->remoteId = md5(uniqid('', true) . microtime());
$createStruct->alwaysAvailable = true;
return $contentService->createContent($createStruct);
}
/**
* Check that the given Content Object contains proper Keywords.
*
* @param int $contentId
* @param \eZ\Publish\Core\FieldType\Keyword\Value $value
*/
private function assertContentFieldHasCorrectData($contentId, KeywordValue $value)
{
$contentService = $this->getRepository()->getContentService();
$loadedContent = $contentService->loadContent($contentId, ['eng-US']);
$dataField = $loadedContent->getField('data');
sort($dataField->value->values);
sort($value->values);
$this->assertEquals($value, $dataField->value);
}
public function testKeywordsAreCaseSensitive()
{
$contentType = $this->testCreateContentType();
$publishedContent01 = $this->createAndPublishContent('Foo', $contentType, md5(uniqid() . microtime()));
$publishedContent02 = $this->createAndPublishContent('foo', $contentType, md5(uniqid() . microtime()));
$data = $publishedContent01->getField('data')->value;
$this->assertCount(1, $data->values);
$this->assertEquals('Foo', $data->values[0]);
$data = $publishedContent02->getField('data')->value;
$this->assertCount(1, $data->values);
$this->assertEquals('foo', $data->values[0]);
}
/**
* Create and publish content of $contentType with $fieldData.
*
* @param mixed $fieldData
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param string $remoteId
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createAndPublishContent($fieldData, ContentType $contentType, $remoteId)
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$createStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
$createStruct->setField('name', 'Test object');
$createStruct->setField(
'data',
$fieldData
);
$createStruct->remoteId = $remoteId;
$createStruct->alwaysAvailable = true;
$contentDraft = $contentService->createContent($createStruct);
return $contentService->publishVersion($contentDraft->versionInfo);
}
protected function getValidSearchValueOne()
{
return 'add';
}
protected function getValidSearchValueTwo()
{
return 'branch';
}
protected function getValidMultivaluedSearchValuesOne()
{
return ['add', 'branch'];
}
protected function getValidMultivaluedSearchValuesTwo()
{
return ['commit', 'delete'];
}
public function checkFullTextSupport()
{
// Does nothing
}
protected function getFullTextIndexedFieldData()
{
return [
['add', 'branch'],
];
}
public function providerForTestTruncateField()
{
return [
[new KeywordValue()],
[new KeywordValue(null)],
[new KeywordValue([])],
// an empty array is what actually REST API sets when field should be truncated
[[]],
];
}
/**
* Test that setting an empty value truncates field data.
*
* @dataProvider providerForTestTruncateField
* @param mixed $emptyValue data representing an empty value
* @todo Move this method to BaseIntegrationTest when fixed for all field types.
*/
public function testTruncateField($emptyValue)
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$fieldType = $repository->getFieldTypeService()->getFieldType($this->getTypeName());
$contentDraft = $this->testCreateContent();
$publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
$contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
$updateStruct = $contentService->newContentUpdateStruct();
$updateStruct->setField('data', $emptyValue);
$contentDraft = $contentService->updateContent($contentDraft->versionInfo, $updateStruct);
$publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
$content = $contentService->loadContent($publishedContent->contentInfo->id, ['eng-US']);
$fieldValue = $content->getField('data')->value;
self::assertTrue(
$fieldType->isEmptyValue($fieldValue),
'Field value is not empty: ' . var_export($fieldValue, true)
);
}
}