forked from ezsystems/ezpublish-kernel
/
TrashServiceTest.php
913 lines (757 loc) · 29.9 KB
/
TrashServiceTest.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
<?php
/**
* File containing the TrashServiceTest 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;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\URLAliasService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location as APILocation;
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Values\Content\Trash\SearchResult;
use eZ\Publish\API\Repository\Values\Content\TrashItem as APITrashItem;
use eZ\Publish\Core\Repository\Values\Content\TrashItem;
use eZ\Publish\Core\Repository\Values\Content\Location;
/**
* Test case for operations in the TrashService using in memory storage.
*
* @see eZ\Publish\API\Repository\TrashService
* @group integration
* @group trash
*/
class TrashServiceTest extends BaseTrashServiceTest
{
/**
* Test for the trash() method.
*
* @see \eZ\Publish\API\Repository\TrashService::trash()
* @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
*/
public function testTrash()
{
/* BEGIN: Use Case */
$trashItem = $this->createTrashItem();
/* END: Use Case */
$this->assertInstanceOf(
'\\eZ\\Publish\\API\\Repository\\Values\\Content\\TrashItem',
$trashItem
);
}
/**
* Test for the trash() method.
*
* @see \eZ\Publish\API\Repository\TrashService::trash()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testTrashSetsExpectedTrashItemProperties()
{
$repository = $this->getRepository();
$mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
// Load the location that will be trashed
$location = $repository->getLocationService()
->loadLocationByRemoteId($mediaRemoteId);
$expected = array(
'id' => $location->id,
'depth' => $location->depth,
'hidden' => $location->hidden,
'invisible' => $location->invisible,
'parentLocationId' => $location->parentLocationId,
'pathString' => $location->pathString,
'priority' => $location->priority,
'remoteId' => $location->remoteId,
'sortField' => $location->sortField,
'sortOrder' => $location->sortOrder,
);
$trashItem = $this->createTrashItem();
$this->assertPropertiesCorrect($expected, $trashItem);
}
/**
* Test for the trash() method.
*
* @see \eZ\Publish\API\Repository\TrashService::trash()
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testTrashRemovesLocationFromMainStorage()
{
$repository = $this->getRepository();
$mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
/* BEGIN: Use Case */
$this->createTrashItem();
// Load the location service
$locationService = $repository->getLocationService();
// This call will fail with a "NotFoundException", because the media
// location was marked as trashed in the main storage
$locationService->loadLocationByRemoteId($mediaRemoteId);
/* END: Use Case */
}
/**
* Test for the trash() method.
*
* @see \eZ\Publish\API\Repository\TrashService::trash()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testTrashRemovesChildLocationsFromMainStorage()
{
$repository = $this->getRepository();
/* BEGIN: Use Case */
$remoteIds = $this->createRemoteIdList();
$this->createTrashItem();
// All invocations to loadLocationByRemoteId() to one of the above
// collected remoteIds will return in an "NotFoundException"
/* END: Use Case */
$locationService = $repository->getLocationService();
foreach ($remoteIds as $remoteId) {
try {
$locationService->loadLocationByRemoteId($remoteId);
$this->fail("Location '{$remoteId}' should exist.'");
} catch (NotFoundException $e) {
// echo $e->getFile(), ' +', $e->getLine(), PHP_EOL;
}
}
$this->assertGreaterThan(
0,
count($remoteIds),
"There should be at least one 'Community' child location."
);
}
/**
* Test for the trash() method.
*
* @see \eZ\Publish\API\Repository\TrashService::trash()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testTrashDecrementsChildCountOnParentLocation()
{
$repository = $this->getRepository();
$locationService = $repository->getLocationService();
$baseLocationId = $this->generateId('location', 1);
$location = $locationService->loadLocation($baseLocationId);
$childCount = $locationService->getLocationChildCount($location);
$this->createTrashItem();
$this->refreshSearch($repository);
$this->assertEquals(
$childCount - 1,
$locationService->getLocationChildCount($location)
);
}
/**
* Test sending a location to trash updates Content mainLocation.
*
* @covers \eZ\Publish\API\Repository\TrashService::trash
*/
public function testTrashUpdatesMainLocation()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$trashService = $repository->getTrashService();
$contentInfo = $contentService->loadContentInfo(42);
// Create additional location that will become new main location
$location = $locationService->createLocation(
$contentInfo,
new LocationCreateStruct(['parentLocationId' => 2])
);
$trashService->trash(
$locationService->loadLocation($contentInfo->mainLocationId)
);
self::assertEquals(
$location->id,
$contentService->loadContentInfo(42)->mainLocationId
);
}
/**
* Test sending a location to trash.
*
* @covers \eZ\Publish\API\Repository\TrashService::trash
*/
public function testTrashReturnsNull()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$trashService = $repository->getTrashService();
// Create additional location to trash
$location = $locationService->createLocation(
$contentService->loadContentInfo(42),
new LocationCreateStruct(['parentLocationId' => 2])
);
$trashItem = $trashService->trash($location);
self::assertNull($trashItem);
}
/**
* Test for the loadTrashItem() method.
*
* @covers \eZ\Publish\API\Repository\TrashService::loadTrashItem
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testLoadTrashItem()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
/* BEGIN: Use Case */
$trashItem = $this->createTrashItem();
// Reload the trash item
$trashItemReloaded = $trashService->loadTrashItem($trashItem->id);
/* END: Use Case */
$this->assertInstanceOf(
APITrashItem::class,
$trashItemReloaded
);
$this->assertEquals(
$trashItem->pathString,
$trashItemReloaded->pathString
);
$this->assertEquals(
$trashItem,
$trashItemReloaded
);
$this->assertInstanceOf(
Content::class,
$content = $trashItemReloaded->getContent()
);
$this->assertEquals($trashItem->contentId, $content->contentInfo->id);
}
/**
* Test for the loadTrashItem() method.
*
* @see \eZ\Publish\API\Repository\TrashService::loadTrashItem()
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testLoadTrashItem
*/
public function testLoadTrashItemThrowsNotFoundException()
{
$repository = $this->getRepository();
$nonExistingTrashId = $this->generateId('trash', 2342);
/* BEGIN: Use Case */
$trashService = $repository->getTrashService();
// This call will fail with a "NotFoundException", because no trash item
// with the ID 1342 should exist in an eZ Publish demo installation
$trashService->loadTrashItem($nonExistingTrashId);
/* END: Use Case */
}
/**
* Test for the recover() method.
*
* @covers \eZ\Publish\API\Repository\TrashService::recover
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testRecover()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$locationService = $repository->getLocationService();
$mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
/* BEGIN: Use Case */
$trashItem = $this->createTrashItem();
// Recover the trashed item
$location = $trashService->recover($trashItem);
// Load the recovered location
$locationReloaded = $locationService->loadLocationByRemoteId(
$mediaRemoteId
);
/* END: Use Case */
$this->assertInstanceOf(
APILocation::class,
$location
);
$this->assertEquals(
$location,
$locationReloaded
);
try {
$trashService->loadTrashItem($trashItem->id);
$this->fail('Trash item was not removed after being recovered.');
} catch (NotFoundException $e) {
// All well
}
}
/**
* Test recovering a non existing trash item results in a NotFoundException.
*
* @covers \eZ\Publish\API\Repository\TrashService::recover
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function testRecoverThrowsNotFoundExceptionForNonExistingTrashItem()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$trashService->recover(
$this->getTrashItemDouble(
12364,
12345,
12363
)
);
}
/**
* Test for the trash() method.
*
* @see \eZ\Publish\API\Repository\TrashService::recover()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function testNotFoundAliasAfterRemoveIt()
{
$mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$urlAliasService = $repository->getURLAliasService();
$locationService = $repository->getLocationService();
// Double ->lookup() call because there where issue that one call was not enough to spot bug
$urlAliasService->lookup('/Media');
$urlAliasService->lookup('/Media');
$mediaLocation = $locationService->loadLocationByRemoteId($mediaRemoteId);
$trashService->trash($mediaLocation);
$urlAliasService->lookup('/Media');
}
/**
* Test for the recover() method.
*
* @see \eZ\Publish\API\Repository\TrashService::recover()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testAliasesForRemovedItems()
{
$mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$urlAliasService = $repository->getURLAliasService();
$locationService = $repository->getLocationService();
// Double ->lookup() call because there where issue that one call was not enough to spot bug
$urlAliasService->lookup('/Media');
$trashedLocationAlias = $urlAliasService->lookup('/Media');
$mediaLocation = $locationService->loadLocationByRemoteId($mediaRemoteId);
$trashItem = $trashService->trash($mediaLocation);
$this->assertAliasNotExists($urlAliasService, '/Media');
$this->createNewContentInPlaceTrashedOne($repository, $mediaLocation->parentLocationId);
$createdLocationAlias = $urlAliasService->lookup('/Media');
$this->assertNotEquals(
$trashedLocationAlias->destination,
$createdLocationAlias->destination,
'Destination for /media url should changed'
);
$recoveredLocation = $trashService->recover($trashItem);
$recoveredLocationAlias = $urlAliasService->lookup('/Media2');
$recoveredLocationAliasReverse = $urlAliasService->reverseLookup($recoveredLocation);
$this->assertEquals($recoveredLocationAlias->destination, $recoveredLocationAliasReverse->destination);
$this->assertNotEquals($recoveredLocationAliasReverse->destination, $trashedLocationAlias->destination);
$this->assertNotEquals($recoveredLocationAliasReverse->destination, $createdLocationAlias->destination);
}
/**
* Test for the recover() method.
*
* @see \eZ\Publish\API\Repository\TrashService::recover()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
*/
public function testRecoverDoesNotRestoreChildLocations()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$locationService = $repository->getLocationService();
$remoteIds = $this->createRemoteIdList();
// Unset remote ID of actually restored location
unset($remoteIds[array_search('3f6d92f8044aed134f32153517850f5a', $remoteIds)]);
$trashItem = $this->createTrashItem();
$trashService->recover($trashItem);
$this->assertGreaterThan(
0,
count($remoteIds),
"There should be at least one 'Community' child location."
);
// None of the child locations will be available again
foreach ($remoteIds as $remoteId) {
try {
$locationService->loadLocationByRemoteId($remoteId);
$this->fail(
sprintf(
'Location with remote ID "%s" unexpectedly restored.',
$remoteId
)
);
} catch (NotFoundException $e) {
// All well
}
}
try {
$trashService->loadTrashItem($trashItem->id);
$this->fail('Trash item was not removed after being recovered.');
} catch (NotFoundException $e) {
// All well
}
}
/**
* Test for the recover() method.
*
* @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation)
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
*
* @todo Fix naming
*/
public function testRecoverWithLocationCreateStructParameter()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$locationService = $repository->getLocationService();
$homeLocationId = $this->generateId('location', 2);
/* BEGIN: Use Case */
// $homeLocationId is the ID of the "Home" location in an eZ Publish
// demo installation
$trashItem = $this->createTrashItem();
// Get the new parent location
$newParentLocation = $locationService->loadLocation($homeLocationId);
// Recover location with new location
$location = $trashService->recover($trashItem, $newParentLocation);
/* END: Use Case */
$this->assertPropertiesCorrect(
array(
'remoteId' => $trashItem->remoteId,
'parentLocationId' => $homeLocationId,
// Not the full sub tree is restored
'depth' => $newParentLocation->depth + 1,
'hidden' => false,
'invisible' => $trashItem->invisible,
'pathString' => $newParentLocation->pathString . $this->parseId('location', $location->id) . '/',
'priority' => 0,
'sortField' => APILocation::SORT_FIELD_NAME,
'sortOrder' => APILocation::SORT_ORDER_ASC,
),
$location
);
try {
$trashService->loadTrashItem($trashItem->id);
$this->fail('Trash item was not removed after being recovered.');
} catch (NotFoundException $e) {
// All well
}
}
/**
* Test for the recover() method.
*
* @see \eZ\Publish\API\Repository\TrashService::recover($trashItem)
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
*/
public function testRecoverIncrementsChildCountOnOriginalParent()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$locationService = $repository->getLocationService();
$location = $locationService->loadLocation($this->generateId('location', 1));
$trashItem = $this->createTrashItem();
$this->refreshSearch($repository);
/* BEGIN: Use Case */
$childCount = $locationService->getLocationChildCount($location);
// Recover location with new location
$trashService->recover($trashItem);
/* END: Use Case */
$this->refreshSearch($repository);
$this->assertEquals(
$childCount + 1,
$locationService->getLocationChildCount($location)
);
try {
$trashService->loadTrashItem($trashItem->id);
$this->fail('Trash item was not removed after being recovered.');
} catch (NotFoundException $e) {
// All well
}
}
/**
* Test for the recover() method.
*
* @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation)
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecoverWithLocationCreateStructParameter
*/
public function testRecoverWithLocationCreateStructParameterIncrementsChildCountOnNewParent()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$locationService = $repository->getLocationService();
$homeLocationId = $this->generateId('location', 2);
$location = $locationService->loadLocation($homeLocationId);
$childCount = $locationService->getLocationChildCount($location);
/* BEGIN: Use Case */
// $homeLocationId is the ID of the "Home" location in an eZ Publish
// demo installation
$trashItem = $this->createTrashItem();
// Get the new parent location
$newParentLocation = $locationService->loadLocation($homeLocationId);
// Recover location with new location
$trashService->recover($trashItem, $newParentLocation);
/* END: Use Case */
$this->refreshSearch($repository);
$this->assertEquals(
$childCount + 1,
$locationService->getLocationChildCount($location)
);
try {
$trashService->loadTrashItem($trashItem->id);
$this->fail('Trash item was not removed after being recovered.');
} catch (NotFoundException $e) {
// All well
}
}
/**
* Test recovering a location from trash to non existing location.
*
* @covers \eZ\Publish\API\Repository\TrashService::recover
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function testRecoverToNonExistingLocation()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$locationService = $repository->getLocationService();
$location = $locationService->loadLocation(44);
$trashItem = $trashService->trash($location);
$newParentLocation = new Location(
array(
'id' => 123456,
'parentLocationId' => 123455,
)
);
$trashService->recover($trashItem, $newParentLocation);
}
/**
* Test for the findTrashItems() method.
*
* @see \eZ\Publish\API\Repository\TrashService::findTrashItems()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
*/
public function testFindTrashItems()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
/* BEGIN: Use Case */
$this->createTrashItem();
// Create a search query for all trashed items
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
)
);
// Load all trashed locations
$searchResult = $trashService->findTrashItems($query);
/* END: Use Case */
$this->assertInstanceOf(
SearchResult::class,
$searchResult
);
// 4 trashed locations from the sub tree
$this->assertEquals(4, $searchResult->count);
$this->assertEquals(4, $searchResult->totalCount);
}
/**
* Test for the findTrashItems() method.
*
* @see \eZ\Publish\API\Repository\TrashService::findTrashItems()
* @depends \eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems
*/
public function testFindTrashItemsLimitedAccess()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
/* BEGIN: Use Case */
$this->createTrashItem();
// Create a search query for all trashed items
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
)
);
// Create a user in the Editor user group.
$user = $this->createUserVersion1();
// Set the Editor user as current user, these users have no access to Trash by default.
$repository->getPermissionResolver()->setCurrentUserReference($user);
// Load all trashed locations
$searchResult = $trashService->findTrashItems($query);
/* END: Use Case */
$this->assertInstanceOf(
'\\eZ\\Publish\\API\\Repository\\Values\\Content\\SearchResult',
$searchResult
);
// 0 trashed locations found, though 4 exist
$this->assertEquals(0, $searchResult->count);
}
/**
* Test for the emptyTrash() method.
*
* @see \eZ\Publish\API\Repository\TrashService::emptyTrash()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems
*/
public function testEmptyTrash()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
/* BEGIN: Use Case */
$this->createTrashItem();
// Empty the trash
$trashService->emptyTrash();
// Create a search query for all trashed items
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
)
);
// Load all trashed locations, search result should be empty
$searchResult = $trashService->findTrashItems($query);
/* END: Use Case */
$this->assertEquals(0, $searchResult->count);
}
/**
* Test for the deleteTrashItem() method.
*
* @see \eZ\Publish\API\Repository\TrashService::deleteTrashItem()
* @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems
*/
public function testDeleteTrashItem()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$locationService = $repository->getLocationService();
$demoDesignLocationId = $this->generateId('location', 56);
/* BEGIN: Use Case */
// $demoDesignLocationId is the ID of the "Demo Design" location in an eZ
// Publish demo installation
$trashItem = $this->createTrashItem();
// Trash one more location
$trashService->trash(
$locationService->loadLocation($demoDesignLocationId)
);
// Empty the trash
$trashService->deleteTrashItem($trashItem);
// Create a search query for all trashed items
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
)
);
// Load all trashed locations, should only contain the Demo Design location
$searchResult = $trashService->findTrashItems($query);
/* END: Use Case */
$foundIds = array_map(
function ($trashItem) {
return $trashItem->id;
},
$searchResult->items
);
$this->assertEquals(4, $searchResult->count);
$this->assertTrue(
in_array($demoDesignLocationId, $foundIds)
);
}
/**
* Test deleting a non existing trash item.
*
* @covers \eZ\Publish\API\Repository\TrashService::deleteTrashItem
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function testDeleteThrowsNotFoundExceptionForNonExistingTrashItem()
{
$repository = $this->getRepository();
$trashService = $repository->getTrashService();
$trashService->deleteTrashItem($this->getTrashItemDouble(
12364,
12345,
12363
));
}
/**
* Returns an array with the remoteIds of all child locations of the
* <b>Community</b> location. It is stored in a local variable named
* <b>$remoteIds</b>.
*
* @return string[]
*/
private function createRemoteIdList()
{
$repository = $this->getRepository();
/* BEGIN: Inline */
// remoteId of the "Community" location in an eZ Publish demo installation
$mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
// Load the location service
$locationService = $repository->getLocationService();
$remoteIds = array();
$children = $locationService->loadLocationChildren($locationService->loadLocationByRemoteId($mediaRemoteId));
foreach ($children->locations as $child) {
$remoteIds[] = $child->remoteId;
foreach ($locationService->loadLocationChildren($child)->locations as $grandChild) {
$remoteIds[] = $grandChild->remoteId;
}
}
/* END: Inline */
return $remoteIds;
}
/**
* @param Repository $repository
* @param int $parentLocationId
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createNewContentInPlaceTrashedOne(Repository $repository, $parentLocationId)
{
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$contentTypeService = $repository->getContentTypeService();
$contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
$newContent = $contentService->newContentCreateStruct($contentType, 'eng-US');
$newContent->setField('name', 'Media');
$location = $locationService->newLocationCreateStruct($parentLocationId);
$draftContent = $contentService->createContent($newContent, [$location]);
return $contentService->publishVersion($draftContent->versionInfo);
}
/**
* @param URLAliasService $urlAliasService
* @param string $urlPath Url alias path
*
* @return \eZ\Publish\API\Repository\Values\Content\URLAlias
*/
private function assertAliasExists(URLAliasService $urlAliasService, $urlPath)
{
$urlAlias = $urlAliasService->lookup($urlPath);
$this->assertInstanceOf('\eZ\Publish\API\Repository\Values\Content\URLAlias', $urlAlias);
return $urlAlias;
}
/**
* @param URLAliasService $urlAliasService
* @param string $urlPath Url alias path
*/
private function assertAliasNotExists(URLAliasService $urlAliasService, $urlPath)
{
try {
$this->getRepository()->getURLAliasService()->lookup($urlPath);
$this->fail(sprintf('Alias [%s] should not exists', $urlPath));
} catch (\eZ\Publish\API\Repository\Exceptions\NotFoundException $e) {
$this->assertTrue(true);
}
}
/**
* Get Test Double for TrashItem for exception testing and similar.
*
* @param int $trashId
* @param int $contentId
* @param int $parentLocationId
*
* @return \eZ\Publish\API\Repository\Values\Content\TrashItem
*/
private function getTrashItemDouble(int $trashId, int $contentId = 44, int $parentLocationId = 2): APITrashItem
{
return new TrashItem([
'id' => $trashId,
'parentLocationId' => $parentLocationId,
'contentInfo' => new ContentInfo(['id' => $contentId]),
]);
}
}