Completed
Push — migrate-files-no-interaction ( 025687...608925 )
by
unknown
46:43 queued 18:48
created

ContentHandlerTest::testSetStatusPublished()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains Test class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache\Tests;
10
11
use eZ\Publish\API\Repository\Values\Content\Relation as APIRelation;
12
use eZ\Publish\SPI\Persistence\Content\Relation as SPIRelation;
13
use eZ\Publish\Core\Persistence\Cache\ContentHandler;
14
use eZ\Publish\SPI\Persistence\Content;
15
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
16
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
17
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
18
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
19
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
20
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
21
use eZ\Publish\SPI\Persistence\Content\Handler as SPIContentHandler;
22
use eZ\Publish\SPI\Persistence\Content\Location\Handler as SPILocationHandler;
23
use Stash\Interfaces\ItemInterface;
24
25
/**
26
 * Test case for Persistence\Cache\ContentHandler.
27
 */
28
class ContentHandlerTest extends HandlerTest
29
{
30
    /**
31
     * @return array
32
     */
33
    public function providerForUnCachedMethods()
34
    {
35
        return array(
36
            array('create', array(new CreateStruct())),
37
            array('createDraftFromVersion', array(2, 1, 14)),
38
            array('copy', array(2, 1)),
39
            //array( 'load', array( 2, 1, array( 'eng-GB' ) ) ),
40
            //array( 'load', array( 2, 1 ) ),
41
            //array( 'loadContentInfo', array( 2 ) ),
42
            array('loadVersionInfo', array(2, 1)),
43
            array('loadDraftsForUser', array(14)),
44
            //array( 'setStatus', array( 2, 0, 1 ) ),
45
            //array( 'updateMetadata', array( 2, new MetadataUpdateStruct ) ),
46
            //array( 'updateContent', array( 2, 1, new UpdateStruct ) ),
47
            //array( 'deleteContent', array( 2 ) ),
48
            //array( 'deleteVersion', array( 2, 1 ) ),
49
            array('listVersions', array(2)),
50
            array('addRelation', array(new RelationCreateStruct())),
51
            array('removeRelation', array(66, APIRelation::COMMON)),
52
            array('loadRelations', array(2, 1, 3)),
53
            array('loadReverseRelations', array(2, 3)),
54
            //array( 'publish', array( 2, 3, new MetadataUpdateStruct ) ),
55
        );
56
    }
57
58
    /**
59
     * @dataProvider providerForUnCachedMethods
60
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler
61
     */
62 View Code Duplication
    public function testUnCachedMethods($method, array $arguments)
63
    {
64
        $this->loggerMock->expects($this->once())->method('logCall');
65
        $this->cacheMock
66
            ->expects($this->never())
67
            ->method($this->anything());
68
69
        $innerHandler = $this->createMock(SPIContentHandler::class);
70
        $this->persistenceHandlerMock
71
            ->expects($this->once())
72
            ->method('contentHandler')
73
            ->will($this->returnValue($innerHandler));
74
75
        $expects = $innerHandler
76
            ->expects($this->once())
77
            ->method($method);
78
79
        if (isset($arguments[2])) {
80
            $expects->with($arguments[0], $arguments[1], $arguments[2]);
81
        } elseif (isset($arguments[1])) {
82
            $expects->with($arguments[0], $arguments[1]);
83
        } elseif (isset($arguments[0])) {
84
            $expects->with($arguments[0]);
85
        }
86
87
        $expects->will($this->returnValue(null));
88
89
        $handler = $this->persistenceCacheHandler->contentHandler();
90
        call_user_func_array(array($handler, $method), $arguments);
91
    }
92
93
    /**
94
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::load
95
     */
96
    public function testLoadCacheIsMiss()
97
    {
98
        $this->loggerMock->expects($this->once())->method('logCall');
99
        $cacheItemMock = $this->createMock(ItemInterface::class);
100
        $this->cacheMock
101
            ->expects($this->once())
102
            ->method('getItem')
103
            ->with('content', 2, 1, 'eng-GB|eng-US')
104
            ->will($this->returnValue($cacheItemMock));
105
106
        $cacheItemMock
107
            ->expects($this->once())
108
            ->method('get')
109
            ->will($this->returnValue(null));
110
111
        $cacheItemMock
112
            ->expects($this->once())
113
            ->method('isMiss')
114
            ->will($this->returnValue(true));
115
116
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
117
        $this->persistenceHandlerMock
118
            ->expects($this->once())
119
            ->method('contentHandler')
120
            ->will($this->returnValue($innerHandlerMock));
121
122
        $innerHandlerMock
123
            ->expects($this->once())
124
            ->method('load')
125
            ->with(2, 1, array('eng-GB', 'eng-US'))
126
            ->will(
127
                $this->returnValue(
128
                    new Content(
129
                        array(
130
                            'fields' => array(),
131
                            'versionInfo' => new VersionInfo(
132
                                array(
133
                                    'versionNo' => 1,
134
                                    'contentInfo' => new ContentInfo(array('id' => 2)),
135
                                )
136
                            ),
137
                        )
138
                    )
139
                )
140
            );
141
142
        $cacheItemMock
143
            ->expects($this->once())
144
            ->method('set')
145
            ->with($this->isInstanceOf(Content::class))
146
            ->will($this->returnValue($cacheItemMock));
147
148
        $cacheItemMock
149
            ->expects($this->once())
150
            ->method('save')
151
            ->with();
152
153
        $handler = $this->persistenceCacheHandler->contentHandler();
154
        $handler->load(2, 1, array('eng-GB', 'eng-US'));
155
    }
156
157
    /**
158
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::load
159
     */
160
    public function testLoadHasCache()
161
    {
162
        $this->loggerMock->expects($this->never())->method($this->anything());
163
        $cacheItemMock = $this->createMock(ItemInterface::class);
164
        $this->cacheMock
165
            ->expects($this->once())
166
            ->method('getItem')
167
            ->with('content', 2, 1, ContentHandler::ALL_TRANSLATIONS_KEY)
168
            ->will($this->returnValue($cacheItemMock));
169
170
        $cacheItemMock
171
            ->expects($this->once())
172
            ->method('isMiss')
173
            ->will($this->returnValue(false));
174
175
        $this->persistenceHandlerMock
176
            ->expects($this->never())
177
            ->method('contentHandler');
178
179
        $cacheItemMock
180
            ->expects($this->once())
181
            ->method('get')
182
            ->will(
183
                $this->returnValue(
184
                    new Content(
185
                        array(
186
                            'fields' => array(),
187
                            'versionInfo' => new VersionInfo(
188
                                array(
189
                                    'versionNo' => 1,
190
                                    'contentInfo' => new ContentInfo(array('id' => 2)),
191
                                )
192
                            ),
193
                        )
194
                    )
195
                )
196
            );
197
198
        $cacheItemMock
199
            ->expects($this->never())
200
            ->method('set');
201
202
        $handler = $this->persistenceCacheHandler->contentHandler();
203
        $handler->load(2, 1);
204
    }
205
206
    /**
207
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::loadContentInfo
208
     */
209
    public function testLoadContentInfoCacheIsMiss()
210
    {
211
        $this->loggerMock->expects($this->once())->method('logCall');
212
        $cacheItemMock = $this->createMock(ItemInterface::class);
213
        $this->cacheMock
214
            ->expects($this->once())
215
            ->method('getItem')
216
            ->with('content', 'info', 2)
217
            ->will($this->returnValue($cacheItemMock));
218
219
        $cacheItemMock
220
            ->expects($this->once())
221
            ->method('get')
222
            ->will($this->returnValue(null));
223
224
        $cacheItemMock
225
            ->expects($this->once())
226
            ->method('isMiss')
227
            ->will($this->returnValue(true));
228
229
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
230
        $this->persistenceHandlerMock
231
            ->expects($this->once())
232
            ->method('contentHandler')
233
            ->will($this->returnValue($innerHandlerMock));
234
235
        $innerHandlerMock
236
            ->expects($this->once())
237
            ->method('loadContentInfo')
238
            ->with(2)
239
            ->will(
240
                $this->returnValue(
241
                    new ContentInfo(array('id' => 2))
242
                )
243
            );
244
245
        $cacheItemMock
246
            ->expects($this->once())
247
            ->method('set')
248
            ->with($this->isInstanceOf(ContentInfo::class))
249
            ->will($this->returnValue($cacheItemMock));
250
251
        $cacheItemMock
252
            ->expects($this->once())
253
            ->method('save')
254
            ->with();
255
256
        $handler = $this->persistenceCacheHandler->contentHandler();
257
        $handler->loadContentInfo(2, 1);
258
    }
259
260
    /**
261
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::loadContentInfo
262
     */
263
    public function testLoadContentInfoHasCache()
264
    {
265
        $this->loggerMock->expects($this->never())->method($this->anything());
266
        $cacheItemMock = $this->createMock(ItemInterface::class);
267
        $this->cacheMock
268
            ->expects($this->once())
269
            ->method('getItem')
270
            ->with('content', 'info', 2)
271
            ->will($this->returnValue($cacheItemMock));
272
273
        $cacheItemMock
274
            ->expects($this->once())
275
            ->method('isMiss')
276
            ->will($this->returnValue(false));
277
278
        $this->persistenceHandlerMock
279
            ->expects($this->never())
280
            ->method('contentHandler');
281
282
        $cacheItemMock
283
            ->expects($this->once())
284
            ->method('get')
285
            ->will(
286
                $this->returnValue(
287
                    new ContentInfo(array('id' => 2))
288
                )
289
            );
290
291
        $cacheItemMock
292
            ->expects($this->never())
293
            ->method('set');
294
295
        $handler = $this->persistenceCacheHandler->contentHandler();
296
        $handler->loadContentInfo(2);
297
    }
298
299
    /**
300
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::setStatus
301
     */
302 View Code Duplication
    public function testSetStatus()
303
    {
304
        $this->loggerMock->expects($this->once())->method('logCall');
305
306
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
307
        $this->persistenceHandlerMock
308
            ->expects($this->once())
309
            ->method('contentHandler')
310
            ->will($this->returnValue($innerHandlerMock));
311
312
        $innerHandlerMock
313
            ->expects($this->once())
314
            ->method('setStatus')
315
            ->with(2, VersionInfo::STATUS_ARCHIVED, 1)
316
            ->will($this->returnValue(true));
317
318
        $this->cacheMock
319
            ->expects($this->once())
320
            ->method('clear')
321
            ->with('content', 2, 1)
322
            ->will($this->returnValue(null));
323
324
        $handler = $this->persistenceCacheHandler->contentHandler();
325
        $handler->setStatus(2, VersionInfo::STATUS_ARCHIVED, 1);
326
    }
327
328
    /**
329
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::setStatus
330
     */
331
    public function testSetStatusPublished()
332
    {
333
        $this->loggerMock->expects($this->once())->method('logCall');
334
335
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
336
        $this->persistenceHandlerMock
337
            ->expects($this->once())
338
            ->method('contentHandler')
339
            ->will($this->returnValue($innerHandlerMock));
340
341
        $innerHandlerMock
342
            ->expects($this->once())
343
            ->method('setStatus')
344
            ->with(2, VersionInfo::STATUS_PUBLISHED, 1)
345
            ->will($this->returnValue(true));
346
347
        $this->cacheMock
348
            ->expects($this->at(0))
349
            ->method('clear')
350
            ->with('content', 2, 1)
351
            ->will($this->returnValue(null));
352
353
        $this->cacheMock
354
            ->expects($this->at(1))
355
            ->method('clear')
356
            ->with('content', 'info', 2)
357
            ->will($this->returnValue(null));
358
359
        $handler = $this->persistenceCacheHandler->contentHandler();
360
        $handler->setStatus(2, VersionInfo::STATUS_PUBLISHED, 1);
361
    }
362
363
    /**
364
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::updateMetadata
365
     */
366
    public function testUpdateMetadata()
367
    {
368
        $this->loggerMock->expects($this->once())->method('logCall');
369
370
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
371
        $this->persistenceHandlerMock
372
            ->expects($this->once())
373
            ->method('contentHandler')
374
            ->will($this->returnValue($innerHandlerMock));
375
376
        $innerHandlerMock
377
            ->expects($this->once())
378
            ->method('updateMetadata')
379
            ->with(2, $this->isInstanceOf(MetadataUpdateStruct::class))
380
            ->willReturn(new ContentInfo(array('id' => 2, 'currentVersionNo' => 3, 'remoteId' => 'o34')));
381
382
        $this->cacheMock
383
            ->expects($this->at(0))
384
            ->method('clear')
385
            ->with('content', 2, 3)
386
            ->willReturn(null);
387
388
        $this->cacheMock
389
            ->expects($this->at(1))
390
            ->method('clear')
391
            ->with('content', 'info', 2)
392
            ->willReturn(null);
393
394
        $this->cacheMock
395
            ->expects($this->at(2))
396
            ->method('clear')
397
            ->with('content', 'info', 'remoteId', 'o34')
398
            ->willReturn(null);
399
400
        $handler = $this->persistenceCacheHandler->contentHandler();
401
        $handler->updateMetadata(2, new MetadataUpdateStruct());
402
    }
403
404
    /**
405
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::updateContent
406
     */
407
    public function testUpdateContent()
408
    {
409
        $this->loggerMock->expects($this->once())->method('logCall');
410
411
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
412
        $this->persistenceHandlerMock
413
            ->expects($this->once())
414
            ->method('contentHandler')
415
            ->will($this->returnValue($innerHandlerMock));
416
417
        $innerHandlerMock
418
            ->expects($this->once())
419
            ->method('updateContent')
420
            ->with(2, 1, $this->isInstanceOf(UpdateStruct::class))
421
            ->will(
422
                $this->returnValue(
423
                    new Content(
424
                        array(
425
                            'fields' => array(),
426
                            'versionInfo' => new VersionInfo(
427
                                array(
428
                                    'versionNo' => 1,
429
                                    'contentInfo' => new ContentInfo(array('id' => 2)),
430
                                )
431
                            ),
432
                        )
433
                    )
434
                )
435
            );
436
437
        $this->cacheMock
438
            ->expects($this->once())
439
            ->method('clear')
440
            ->with('content', 2, 1)
441
            ->will($this->returnValue(null));
442
443
        $handler = $this->persistenceCacheHandler->contentHandler();
444
        $handler->updateContent(2, 1, new UpdateStruct());
445
    }
446
447
    /**
448
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::deleteContent
449
     */
450
    public function testDeleteContent()
451
    {
452
        $this->loggerMock->expects($this->once())->method('logCall');
453
454
        $innerLocationHandlerMock = $this->createMock(SPILocationHandler::class);
455
        $this->persistenceHandlerMock
456
            ->expects($this->exactly(1))
457
            ->method('locationHandler')
458
            ->will($this->returnValue($innerLocationHandlerMock));
459
460
        $innerLocationHandlerMock
461
            ->expects($this->once())
462
            ->method('loadLocationsByContent')
463
            ->with(2)
464
            ->willReturn([new Content\Location(array('id' => 58))]);
465
466
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
467
        $this->persistenceHandlerMock
468
            ->expects($this->exactly(2))
469
            ->method('contentHandler')
470
            ->will($this->returnValue($innerHandlerMock));
471
472
        $innerHandlerMock
473
            ->expects($this->once())
474
            ->method('loadReverseRelations')
475
            ->with(2, APIRelation::FIELD)
476
            ->will(
477
                $this->returnValue(
478
                    array(
479
                        new SPIRelation(array('sourceContentId' => 42)),
480
                    )
481
                )
482
            );
483
484
        $innerHandlerMock
485
            ->expects($this->once())
486
            ->method('deleteContent')
487
            ->with(2)
488
            ->will($this->returnValue(true));
489
490
        $this->cacheMock
491
            ->expects($this->at(0))
492
            ->method('clear')
493
            ->with('content', 42)
494
            ->will($this->returnValue(null));
495
496
        $this->cacheMock
497
            ->expects($this->at(1))
498
            ->method('clear')
499
            ->with('content', 2)
500
            ->will($this->returnValue(null));
501
502
        $this->cacheMock
503
            ->expects($this->at(2))
504
            ->method('clear')
505
            ->with('content', 'info', 2)
506
            ->will($this->returnValue(null));
507
508
        $this->cacheMock
509
            ->expects($this->at(3))
510
            ->method('clear')
511
            ->with('content', 'info', 'remoteId')
512
            ->will($this->returnValue(null));
513
514
        $this->cacheMock
515
            ->expects($this->at(4))
516
            ->method('clear')
517
            ->with('location', 'subtree')
518
            ->will($this->returnValue(null));
519
520
        $this->cacheMock
521
            ->expects($this->at(5))
522
            ->method('clear')
523
            ->with('location', 58)
524
            ->will($this->returnValue(null));
525
526
        $handler = $this->persistenceCacheHandler->contentHandler();
527
        $handler->deleteContent(2);
528
    }
529
530
    /**
531
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::deleteVersion
532
     */
533
    public function testDeleteVersion()
534
    {
535
        $this->loggerMock->expects($this->once())->method('logCall');
536
537
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
538
        $this->persistenceHandlerMock
539
            ->expects($this->once())
540
            ->method('contentHandler')
541
            ->will($this->returnValue($innerHandlerMock));
542
543
        $innerHandlerMock
544
            ->expects($this->once())
545
            ->method('deleteVersion')
546
            ->with(2, 1)
547
            ->will($this->returnValue(true));
548
549
        $this->cacheMock
550
            ->expects($this->at(0))
551
            ->method('clear')
552
            ->with('content', 2, 1)
553
            ->will($this->returnValue(null));
554
555
        $this->cacheMock
556
            ->expects($this->at(1))
557
            ->method('clear')
558
            ->with('content', 'info', 2)
559
            ->will($this->returnValue(null));
560
561
        $this->cacheMock
562
            ->expects($this->at(2))
563
            ->method('clear')
564
            ->with('content', 'info', 'remoteId')
565
            ->will($this->returnValue(null));
566
567
        $this->cacheMock
568
            ->expects($this->at(3))
569
            ->method('clear')
570
            ->with('location', 'subtree')
571
            ->will($this->returnValue(null));
572
573
        $handler = $this->persistenceCacheHandler->contentHandler();
574
        $handler->deleteVersion(2, 1);
575
    }
576
577
    /**
578
     * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::publish
579
     */
580
    public function testPublish()
581
    {
582
        $this->loggerMock->expects($this->once())->method('logCall');
583
584
        $innerHandlerMock = $this->createMock(SPIContentHandler::class);
585
        $this->persistenceHandlerMock
586
            ->expects($this->once())
587
            ->method('contentHandler')
588
            ->will($this->returnValue($innerHandlerMock));
589
590
        $innerHandlerMock
591
            ->expects($this->once())
592
            ->method('publish')
593
            ->with(2, 1, $this->isInstanceOf(MetadataUpdateStruct::class))
594
            ->will(
595
                $this->returnValue(
596
                    new Content(
597
                        array(
598
                            'fields' => array(),
599
                            'versionInfo' => new VersionInfo(
600
                                array(
601
                                    'versionNo' => 1,
602
                                    'contentInfo' => new ContentInfo(array('id' => 2)),
603
                                )
604
                            ),
605
                        )
606
                    )
607
                )
608
            );
609
610
        $this->cacheMock
611
            ->expects($this->at(0))
612
            ->method('clear')
613
            ->with('content', 2)
614
            ->will($this->returnValue(true));
615
616
        $this->cacheMock
617
            ->expects($this->at(1))
618
            ->method('clear')
619
            ->with('content', 'info', 'remoteId')
620
            ->will($this->returnValue(true));
621
622
        $this->cacheMock
623
            ->expects($this->at(2))
624
            ->method('clear')
625
            ->with('location', 'subtree')
626
            ->will($this->returnValue(true));
627
628
        $cacheItemMock = $this->createMock(ItemInterface::class);
629
        $this->cacheMock
630
            ->expects($this->at(3))
631
            ->method('getItem')
632
            ->with('content', 2, 1, ContentHandler::ALL_TRANSLATIONS_KEY)
633
            ->will($this->returnValue($cacheItemMock));
634
635
        $cacheItemMock
636
            ->expects($this->once())
637
            ->method('set')
638
            ->with($this->isInstanceOf(Content::class))
639
            ->will($this->returnValue($cacheItemMock));
640
641
        $cacheItemMock
642
            ->expects($this->once())
643
            ->method('save')
644
            ->with();
645
646
        $cacheItemMock
647
            ->expects($this->never())
648
            ->method('get');
649
650
        $cacheItemMock2 = $this->createMock(ItemInterface::class);
651
        $this->cacheMock
652
            ->expects($this->at(4))
653
            ->method('getItem')
654
            ->with('content', 'info', 2)
655
            ->will($this->returnValue($cacheItemMock2));
656
657
        $cacheItemMock2
658
            ->expects($this->once())
659
            ->method('set')
660
            ->with($this->isInstanceOf(ContentInfo::class))
661
            ->will($this->returnValue($cacheItemMock2));
662
663
        $cacheItemMock2
664
            ->expects($this->once())
665
            ->method('save')
666
            ->with();
667
668
        $cacheItemMock2
669
            ->expects($this->never())
670
            ->method('get');
671
672
        $handler = $this->persistenceCacheHandler->contentHandler();
673
        $handler->publish(2, 1, new MetadataUpdateStruct());
674
    }
675
}
676