forked from ezsystems/ezpublish-api
/
RichTextIntegrationTest.php
925 lines (832 loc) · 31.3 KB
/
RichTextIntegrationTest.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
<?php
/**
* File contains: eZ\Publish\API\Repository\Tests\FieldType\RichTextIntegrationTest 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 DirectoryIterator;
use eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\FieldType\RichText\Value as RichTextValue;
use eZ\Publish\API\Repository\Values\Content\Field;
use DOMDocument;
use eZ\Publish\Core\Repository\Values\Content\Relation;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\SPI\FieldType\ValidationError;
/**
* Integration test for use field type.
*
* @group integration
* @group field-type
*/
class RichTextIntegrationTest extends SearchBaseIntegrationTest
{
use RelationSearchBaseIntegrationTestTrait;
/**
* @var \DOMDocument
*/
private $createdDOMValue;
/**
* @var \DOMDocument
*/
private $updatedDOMValue;
public function __construct($name = null, array $data = array(), $dataName = '')
{
$this->createdDOMValue = new DOMDocument();
$this->createdDOMValue->loadXML(<<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para><link xlink:href="ezlocation://58" xlink:show="none">link1</link></para>
<para><link xlink:href="ezcontent://54" xlink:show="none">link2</link> <ezembedinline xlink:href="ezlocation://60" view="embed" xml:id="embed-id-1" ezxhtml:class="embed-class" ezxhtml:align="left"></ezembedinline></para>
</section>
EOT
);
$this->updatedDOMValue = new DOMDocument();
$this->updatedDOMValue->loadXML(<<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para><link xlink:href="ezlocation://60" xlink:show="none">link1</link></para>
<para><link xlink:href="ezcontent://56" xlink:show="none">link2</link></para>
<ezembed xlink:href="ezcontent://54" view="embed" xml:id="embed-id-1" ezxhtml:class="embed-class" ezxhtml:align="left">
<ezconfig>
<ezvalue key="size">medium</ezvalue>
<ezvalue key="offset">10</ezvalue>
<ezvalue key="limit">5</ezvalue>
</ezconfig>
</ezembed>
</section>
EOT
);
parent::__construct($name, $data, $dataName);
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
*
* @return \eZ\Publish\Core\Repository\Values\Content\Relation[]
*/
public function getCreateExpectedRelations(Content $content)
{
$contentService = $this->getRepository()->getContentService();
return array(
new Relation(
array(
'type' => Relation::LINK,
'sourceContentInfo' => $content->contentInfo,
'destinationContentInfo' => $contentService->loadContentInfo(56),
)
),
new Relation(
array(
'type' => Relation::LINK,
'sourceContentInfo' => $content->contentInfo,
'destinationContentInfo' => $contentService->loadContentInfo(54),
)
),
new Relation(
array(
'type' => Relation::EMBED,
'sourceContentInfo' => $content->contentInfo,
'destinationContentInfo' => $contentService->loadContentInfo(58),
)
),
);
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
*
* @return \eZ\Publish\Core\Repository\Values\Content\Relation[]
*/
public function getUpdateExpectedRelations(Content $content)
{
$contentService = $this->getRepository()->getContentService();
return array(
new Relation(
array(
'type' => Relation::LINK,
'sourceContentInfo' => $content->contentInfo,
'destinationContentInfo' => $contentService->loadContentInfo(58),
)
),
new Relation(
array(
'type' => Relation::LINK,
'sourceContentInfo' => $content->contentInfo,
'destinationContentInfo' => $contentService->loadContentInfo(56),
)
),
new Relation(
array(
// @todo Won't be possible to add before we break how we store relations with legacy kernel.
//'sourceFieldDefinitionIdentifier' => 'data',
'type' => Relation::EMBED,
'sourceContentInfo' => $content->contentInfo,
'destinationContentInfo' => $contentService->loadContentInfo(54),
)
),
);
}
/**
* Get name of tested field type.
*
* @return string
*/
public function getTypeName()
{
return 'ezrichtext';
}
/**
* Get expected settings schema.
*
* @return array
*/
public function getSettingsSchema()
{
return [];
}
/**
* Get a valid $fieldSettings value.
*
* @return mixed
*/
public function getValidFieldSettings()
{
return [];
}
/**
* 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 RichTextValue($this->createdDOMValue);
}
/**
* Get name generated by the given field type (either via Nameable or fieldType->getName()).
*
* @return string
*/
public function getFieldName()
{
return 'link1 link2';
}
/**
* 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\\RichText\\Value',
$field->value
);
$this->assertPropertiesCorrect(
array(
'xml' => $this->createdDOMValue,
),
$field->value
);
}
/**
* 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',
),
);
}
/**
* Get update field externals data.
*
* @return array
*/
public function getValidUpdateFieldData()
{
return new RichTextValue($this->updatedDOMValue);
}
/**
* 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\\RichText\\Value',
$field->value
);
$this->assertPropertiesCorrect(
array(
'xml' => $this->updatedDOMValue,
),
$field->value
);
}
/**
* 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\\RichText\\Value',
$field->value
);
$this->assertPropertiesCorrect(
array(
'xml' => $this->createdDOMValue,
),
$field->value
);
}
/**
* 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()
{
$xml = new DOMDocument();
$xml->loadXML(<<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<title>Some text</title>
<para>Foobar</para>
</section>
EOT
);
return array(
array(
new RichTextValue($xml),
array('xml' => $xml->saveXML()),
),
);
}
/**
* 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(
'xml' => <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<title>Some text</title>
<para>Foobar</para>
</section>
EOT
),
),
);
}
/**
* @dataProvider provideFromHashData
* @todo: Requires correct registered FieldTypeService, needs to be
* maintained!
*/
public function testFromHash($hash, $expectedValue = null)
{
$richTextValue = $this
->getRepository()
->getFieldTypeService()
->getFieldType($this->getTypeName())
->fromHash($hash);
$this->assertInstanceOf(
'eZ\\Publish\\Core\\FieldType\\RichText\\Value',
$richTextValue
);
$this->assertInstanceOf('DOMDocument', $richTextValue->xml);
$this->assertEquals($hash['xml'], (string)$richTextValue);
}
public function providerForTestIsEmptyValue()
{
$xml = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"/>
EOT;
return array(
array(new RichTextValue()),
array(new RichTextValue($xml)),
);
}
public function providerForTestIsNotEmptyValue()
{
$xml = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"> </section>
EOT;
$xml2 = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para/>
</section>
EOT;
return array(
array(
$this->getValidCreationFieldData(),
),
array(new RichTextValue($xml)),
array(new RichTextValue($xml2)),
);
}
/**
* Get data to test remote id conversion.
*
* This is a PHP Unit data provider
*
* @see testConvertReomoteObjectIdToObjectId()
*/
public function providerForTestConvertRemoteObjectIdToObjectId()
{
$remoteId = '[RemoteId]';
$objectId = '[ObjectId]';
return array(
array(
// test link
'<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
<link xlink:href="ezremote://' . $remoteId . '#fragment">link</link>
</para>
</section>',
'<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
<link xlink:href="ezcontent://' . $objectId . '#fragment">link</link>
</para>
</section>
',
), /*, @TODO adapt and enable when embeds are implemented with remote id support
array(
// test embed
'<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
<embed view="embed" size="medium" object_remote_id="' . $remoteId . '"/>
</para>
</section>'
,
'<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
<embed view="embed" size="medium" object_id="' . $objectId . '"/>
</para>
</section>'
),
array(
// test embed-inline
'<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
<embed-inline size="medium" object_remote_id="' . $remoteId . '"/>
</para>
</section>',
'<?xml version="1.0" encoding="utf-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
<embed-inline size="medium" object_id="' . $objectId . '"/>
</para>
</section>'
),
*/
);
}
/**
* This tests the conversion from remote_object_id to object_id.
*
* @dataProvider providerForTestConvertRemoteObjectIdToObjectId
*/
public function testConvertRemoteObjectIdToObjectId($test, $expected)
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$contentTypeService = $repository->getContentTypeService();
$locationService = $repository->getLocationService();
// Create Type containing RichText Field definition
$createStruct = $contentTypeService->newContentTypeCreateStruct('test-RichText');
$createStruct->mainLanguageCode = 'eng-GB';
$createStruct->remoteId = 'test-RichText-abcdefghjklm9876543210';
$createStruct->names = array('eng-GB' => 'Test');
$createStruct->creatorId = $repository->getCurrentUser()->id;
$createStruct->creationDate = $this->createDateTime();
$fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('description', 'ezrichtext');
$fieldCreate->names = array('eng-GB' => 'Title');
$fieldCreate->fieldGroup = 'main';
$fieldCreate->position = 1;
$fieldCreate->isTranslatable = true;
$createStruct->addFieldDefinition($fieldCreate);
$contentGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content');
$contentTypeDraft = $contentTypeService->createContentType($createStruct, array($contentGroup));
$contentTypeService->publishContentTypeDraft($contentTypeDraft);
$testContentType = $contentTypeService->loadContentType($contentTypeDraft->id);
// Create a folder for tests
$createStruct = $contentService->newContentCreateStruct(
$contentTypeService->loadContentTypeByIdentifier('folder'),
'eng-GB'
);
$createStruct->setField('name', 'Folder Link');
$draft = $contentService->createContent(
$createStruct,
array($locationService->newLocationCreateStruct(2))
);
$folder = $contentService->publishVersion(
$draft->versionInfo
);
$objectId = $folder->versionInfo->contentInfo->id;
$locationId = $folder->versionInfo->contentInfo->mainLocationId;
$remoteId = $folder->versionInfo->contentInfo->remoteId;
// Create value to be tested
$testStruct = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
$testStruct->setField('description', str_replace('[RemoteId]', $remoteId, $test));
$test = $contentService->createContent(
$testStruct,
array($locationService->newLocationCreateStruct($locationId))
);
$this->assertEquals(
str_replace('[ObjectId]', $objectId, $expected),
$test->getField('description')->value->xml->saveXML()
);
}
/**
* @param string $xmlDocumentPath
* @dataProvider providerForTestCreateContentWithValidCustomTag
*/
public function testCreateContentWithValidCustomTag($xmlDocumentPath)
{
$validXmlDocument = $this->createDocument($xmlDocumentPath);
$this->createContent(new RichTextValue($validXmlDocument));
}
/**
* Data provider for testCreateContentWithValidCustomTag.
*
* @return array
*/
public function providerForTestCreateContentWithValidCustomTag()
{
$data = [];
$iterator = new DirectoryIterator(__DIR__ . '/_fixtures/ezrichtext/custom_tags/valid');
foreach ($iterator as $fileInfo) {
if ($fileInfo->isFile() && $fileInfo->getExtension() === 'xml') {
$data[] = [
$fileInfo->getRealPath(),
];
}
}
return $data;
}
/**
* @param string $xmlDocumentPath
* @param string $expectedValidationMessage
*
* @dataProvider providerForTestCreateContentWithInvalidCustomTag
*/
public function testCreateContentWithInvalidCustomTag(
$xmlDocumentPath,
$expectedValidationMessage
) {
try {
$invalidXmlDocument = $this->createDocument($xmlDocumentPath);
$this->createContent(new RichTextValue($invalidXmlDocument));
} catch (ContentFieldValidationException $e) {
// get first nested ValidationError
/** @var \eZ\Publish\SPI\FieldType\ValidationError $error */
$error = current(current(current($e->getFieldErrors())));
self::assertEquals(
$expectedValidationMessage,
$error->getTranslatableMessage()->message
);
return;
}
self::fail("Expected ValidationError '{$expectedValidationMessage}' did not occur.");
}
/**
* Data provider for testCreateContentWithInvalidCustomTag.
*
* @return array
*/
public function providerForTestCreateContentWithInvalidCustomTag()
{
$data = [
[
__DIR__ . '/_fixtures/ezrichtext/custom_tags/invalid/unknown_tag.xml',
"Unknown RichText Custom Tag 'unknown_tag'",
],
[
__DIR__ . '/_fixtures/ezrichtext/custom_tags/invalid/equation.xml',
"The attribute 'processor' of RichText Custom Tag 'equation' cannot be empty",
],
[
__DIR__ . '/_fixtures/ezrichtext/custom_tags/invalid/video.xml',
"Unknown attribute 'unknown_attribute' of RichText Custom Tag 'video'",
],
];
return $data;
}
/**
* @param string $filename
*
* @return \DOMDocument
*/
protected function createDocument($filename)
{
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
$document->formatOutput = false;
$document->loadXml(file_get_contents($filename), LIBXML_NOENT);
return $document;
}
/**
* Prepare Content structure with link to deleted Location.
*
* @param \eZ\Publish\API\Repository\Repository $repository
*
* @return array [$deletedLocation, $brokenContent]
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function prepareInternalLinkValidatorBrokenLinksTestCase(Repository $repository)
{
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
// Create first content with single Language
$primaryContent = $contentService->publishVersion(
$this->createMultilingualContent(
['eng-US' => 'ContentA'],
['eng-US' => $this->getValidCreationFieldData()],
[$locationService->newLocationCreateStruct(2)]
)->versionInfo
);
// Create secondary Location (to be deleted) for the first Content
$deletedLocation = $locationService->createLocation(
$primaryContent->contentInfo,
$locationService->newLocationCreateStruct(60)
);
// Create second Content with two Languages, one of them linking to secondary Location
$brokenContent = $contentService->publishVersion(
$this->createMultilingualContent(
[
'eng-US' => 'ContentB',
'eng-GB' => 'ContentB',
],
[
'eng-US' => $this->getValidCreationFieldData(),
'eng-GB' => $this->getDocumentWithLocationLink($deletedLocation),
],
[$locationService->newLocationCreateStruct(2)]
)->versionInfo
);
// delete Location making second Content broken
$locationService->deleteLocation($deletedLocation);
return [$deletedLocation, $brokenContent];
}
/**
* Test updating Content which contains links to deleted Location doesn't fail when updating not broken field only.
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testInternalLinkValidatorIgnoresMissingRelationOnNotUpdatedField()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
list(, $contentB) = $this->prepareInternalLinkValidatorBrokenLinksTestCase($repository);
// update field w/o erroneous link to trigger validation
$contentUpdateStruct = $contentService->newContentUpdateStruct();
$contentUpdateStruct->setField('data', $this->getValidUpdateFieldData(), 'eng-US');
$contentDraftB = $contentService->updateContent(
$contentService->createContentDraft($contentB->contentInfo)->versionInfo,
$contentUpdateStruct
);
$contentService->publishVersion($contentDraftB->versionInfo);
}
/**
* Test updating Content which contains links to deleted Location fails when updating broken field.
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
*/
public function testInternalLinkValidatorReturnsErrorOnMissingRelationInUpdatedField()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
list($deletedLocation, $brokenContent) = $this->prepareInternalLinkValidatorBrokenLinksTestCase(
$repository
);
// update field containing erroneous link to trigger validation
/** @var \DOMDocument $document */
$document = $brokenContent->getField('data', 'eng-GB')->value->xml;
$newParagraph = $document->createElement('para', 'Updated content');
$document
->getElementsByTagName('section')->item(0)
->appendChild($newParagraph);
$contentUpdateStruct = $contentService->newContentUpdateStruct();
$contentUpdateStruct->setField('data', new RichTextValue($document), 'eng-GB');
$expectedValidationErrorMessage = sprintf(
'Invalid link "ezlocation://%s": target location cannot be found',
$deletedLocation->id
);
try {
$contentDraftB = $contentService->updateContent(
$contentService->createContentDraft($brokenContent->contentInfo)->versionInfo,
$contentUpdateStruct
);
$contentService->publishVersion($contentDraftB->versionInfo);
} catch (ContentFieldValidationException $e) {
$this->assertValidationErrorOccurs($e, $expectedValidationErrorMessage);
return;
}
self::fail("Expected ValidationError '{$expectedValidationErrorMessage}' didn't occur");
}
/**
* Get XML Document in DocBook format, containing link to the given Location.
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return \DOMDocument
*/
private function getDocumentWithLocationLink(Location $location)
{
$document = new DOMDocument();
$document->loadXML(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para><link xlink:href="ezlocation://{$location->id}" xlink:show="none">link1</link></para>
</section>
XML
);
return $document;
}
protected function checkSearchEngineSupport()
{
if (ltrim(get_class($this->getSetupFactory()), '\\') === 'eZ\\Publish\\API\\Repository\\Tests\\SetupFactory\\Legacy') {
$this->markTestSkipped(
"'ezrichtext' field type is not searchable with Legacy Search Engine"
);
}
}
protected function getValidSearchValueOne()
{
return <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>caution is the path to mediocrity</para>
</section>
EOT;
}
protected function getSearchTargetValueOne()
{
// ensure case-insensitivity
return strtoupper('caution is the path to mediocrity');
}
protected function getValidSearchValueTwo()
{
return <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>truth suffers from too much analysis</para>
</section>
EOT;
}
protected function getSearchTargetValueTwo()
{
// ensure case-insensitivity
return strtoupper('truth suffers from too much analysis');
}
protected function getFullTextIndexedFieldData()
{
return array(
array('mediocrity', 'analysis'),
);
}
}