Completed
Push — EZP-30852 ( ff098a )
by
unknown
19:08
created

testDeleteSectionThrowsNotFoundException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the SectionServiceTest 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\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12
use eZ\Publish\API\Repository\Values\Content\Section;
13
use Exception;
14
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
15
use eZ\Publish\API\Repository\Values\Content\SectionCreateStruct;
16
use eZ\Publish\API\Repository\Values\Content\SectionUpdateStruct;
17
18
/**
19
 * Test case for operations in the SectionService using in memory storage.
20
 *
21
 * @see eZ\Publish\API\Repository\SectionService
22
 * @group integration
23
 * @group section
24
 */
25
class SectionServiceTest extends BaseTest
26
{
27
    private const SECTION_UNIQUE_KEY = 'uniqueKey';
28
29
    /** @var \eZ\Publish\API\Repository\PermissionResolver */
30
    protected $permissionResolver;
31
32
    /**
33
     * Tests that the required <b>ContentService::loadContentInfoByRemoteId()</b>
34
     * at least returns an object, because this method is utilized in several
35
     * tests,.
36
     */
37
    protected function setUp(): void
38
    {
39
        parent::setUp();
40
41
        try {
42
            // RemoteId of the "Media" page of an eZ Publish demo installation
43
            $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
44
45
            // Load the ContentService
46
            $contentService = $this->getRepository()->getContentService();
47
48
            // Load a content info instance
49
            $contentInfo = $contentService->loadContentInfoByRemoteId(
50
                $mediaRemoteId
51
            );
52
53
            if (false === is_object($contentInfo)) {
54
                $this->markTestSkipped(
55
                    'This test cannot be executed, because the utilized ' .
56
                    'ContentService::loadContentInfoByRemoteId() does not ' .
57
                    'return an object.'
58
                );
59
            }
60
        } catch (Exception $e) {
61
            $this->markTestSkipped(
62
                'This test cannot be executed, because the utilized ' .
63
                'ContentService::loadContentInfoByRemoteId() failed with ' .
64
                PHP_EOL . PHP_EOL .
65
                $e
66
            );
67
        }
68
69
        $repository = $this->getRepository(false);
70
        $this->permissionResolver = $repository->getPermissionResolver();
71
    }
72
73
    /**
74
     * Test for the newSectionCreateStruct() method.
75
     *
76
     * @see \eZ\Publish\API\Repository\SectionService::newSectionCreateStruct()
77
     */
78
    public function testNewSectionCreateStruct()
79
    {
80
        $repository = $this->getRepository();
81
82
        /* BEGIN: Use Case */
83
        $sectionService = $repository->getSectionService();
84
85
        $sectionCreate = $sectionService->newSectionCreateStruct();
86
        /* END: Use Case */
87
88
        $this->assertInstanceOf(SectionCreateStruct::class, $sectionCreate);
89
    }
90
91
    /**
92
     * Test for the createSection() method.
93
     *
94
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
95
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionCreateStruct
96
     */
97
    public function testCreateSection()
98
    {
99
        $repository = $this->getRepository();
100
101
        /* BEGIN: Use Case */
102
        $sectionService = $repository->getSectionService();
103
104
        $sectionCreate = $sectionService->newSectionCreateStruct();
105
        $sectionCreate->name = 'Test Section';
106
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
107
108
        $section = $sectionService->createSection($sectionCreate);
109
        /* END: Use Case */
110
111
        $this->assertInstanceOf(Section::class, $section);
112
    }
113
114
    /**
115
     * Test for the createSection() method.
116
     *
117
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
118
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionCreateStruct
119
     */
120
    public function testCreateSectionForUserWithSectionLimitation()
121
    {
122
        $repository = $this->getRepository();
123
124
        /* BEGIN: Use Case */
125
        $sectionService = $repository->getSectionService();
126
127
        $sectionCreate = $sectionService->newSectionCreateStruct();
128
        $sectionCreate->name = 'Test Section';
129
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
130
131
        $this->createRoleWithPolicies('sectionCreator', [
132
            ['module' => 'section', 'function' => 'edit'],
133
        ]);
134
135
        $user = $this->createCustomUserWithLogin(
136
            'user',
137
            '[email protected]',
138
            'sectionCreators',
139
            'sectionCreator',
140
            new SectionLimitation(['limitationValues' => [1]])
141
        );
142
143
        $repository->getPermissionResolver()->setCurrentUserReference($user);
144
145
        $section = $sectionService->createSection($sectionCreate);
146
        /* END: Use Case */
147
148
        $this->assertInstanceOf(Section::class, $section);
149
        $this->assertSame(self::SECTION_UNIQUE_KEY, $section->identifier);
150
    }
151
152
    /**
153
     * Test for the createSection() method.
154
     *
155
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
156
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
157
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
158
     */
159
    public function testCreateSectionThrowsInvalidArgumentException()
160
    {
161
        $repository = $this->getRepository();
162
163
        /* BEGIN: Use Case */
164
        $sectionService = $repository->getSectionService();
165
166
        $sectionCreateOne = $sectionService->newSectionCreateStruct();
167
        $sectionCreateOne->name = 'Test section one';
168
        $sectionCreateOne->identifier = self::SECTION_UNIQUE_KEY;
169
170
        $sectionService->createSection($sectionCreateOne);
171
172
        $sectionCreateTwo = $sectionService->newSectionCreateStruct();
173
        $sectionCreateTwo->name = 'Test section two';
174
        $sectionCreateTwo->identifier = self::SECTION_UNIQUE_KEY;
175
176
        // This will fail, because identifier uniqueKey already exists.
177
        $sectionService->createSection($sectionCreateTwo);
178
        /* END: Use Case */
179
    }
180
181
    /**
182
     * Test for the loadSection() method.
183
     *
184
     * @see \eZ\Publish\API\Repository\SectionService::loadSection()
185
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
186
     */
187
    public function testLoadSection()
188
    {
189
        $repository = $this->getRepository();
190
191
        $sectionId = $this->generateId('section', 2);
192
        /* BEGIN: Use Case */
193
        $sectionService = $repository->getSectionService();
194
195
        // Loads user section
196
        // $sectionId contains the corresponding ID
197
        $section = $sectionService->loadSection($sectionId);
198
        /* END: Use Case */
199
200
        $this->assertEquals('users', $section->identifier);
201
    }
202
203
    /**
204
     * Test for the loadSection() method.
205
     *
206
     * @see \eZ\Publish\API\Repository\SectionService::loadSection()
207
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
208
     */
209
    public function testLoadSectionThrowsNotFoundException()
210
    {
211
        $repository = $this->getRepository();
212
213
        $nonExistentSectionId = $this->generateId('section', self::DB_INT_MAX);
214
        /* BEGIN: Use Case */
215
        $sectionService = $repository->getSectionService();
216
217
        // This call should fail with a NotFoundException
218
        // $nonExistentSectionId contains a section ID that is not known
219
        $sectionService->loadSection($nonExistentSectionId);
220
        /* END: Use Case */
221
    }
222
223
    /**
224
     * Test for the newSectionUpdateStruct() method.
225
     *
226
     * @see \eZ\Publish\API\Repository\SectionService::newSectionUpdateStruct()
227
     */
228
    public function testNewSectionUpdateStruct()
229
    {
230
        $repository = $this->getRepository();
231
232
        /* BEGIN: Use Case */
233
        $sectionService = $repository->getSectionService();
234
235
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
236
        /* END: Use Case */
237
238
        $this->assertInstanceOf(SectionUpdateStruct::class, $sectionUpdate);
239
    }
240
241
    /**
242
     * Test for the updateSection() method.
243
     *
244
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
245
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
246
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
247
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionUpdateStruct
248
     */
249
    public function testUpdateSection()
250
    {
251
        $repository = $this->getRepository();
252
253
        $standardSectionId = $this->generateId('section', 1);
254
        /* BEGIN: Use Case */
255
        // $standardSectionId contains the ID of the "Standard" section in a eZ
256
        // Publish demo installation.
257
258
        $sectionService = $repository->getSectionService();
259
260
        $section = $sectionService->loadSection($standardSectionId);
261
262
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
263
        $sectionUpdate->name = 'New section name';
264
        $sectionUpdate->identifier = 'newUniqueKey';
265
266
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
267
        /* END: Use Case */
268
269
        // Verify that service returns an instance of Section
270
        $this->assertInstanceOf(Section::class, $updatedSection);
271
272
        // Verify that the service also persists the changes
273
        $updatedSection = $sectionService->loadSection($standardSectionId);
274
275
        $this->assertEquals('New section name', $updatedSection->name);
276
    }
277
278
    /**
279
     * Test for the updateSection() method.
280
     *
281
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
282
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
283
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
284
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionUpdateStruct
285
     */
286
    public function testUpdateSectionForUserWithSectionLimitation()
287
    {
288
        $repository = $this->getRepository();
289
        $administratorUserId = $this->generateId('user', 14);
290
        /* BEGIN: Use Case */
291
        // $standardSectionId contains the ID of the "Standard" section in a eZ
292
        // Publish demo installation.
293
294
        $sectionService = $repository->getSectionService();
295
        $userService = $repository->getUserService();
296
297
        $sectionCreate = $sectionService->newSectionCreateStruct();
298
        $sectionCreate->name = 'Test Section';
299
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
300
        $section = $sectionService->createSection($sectionCreate);
301
302
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
303
        $sectionUpdate->name = 'New section name';
304
        $sectionUpdate->identifier = 'newUniqueKey';
305
306
        $this->createRoleWithPolicies('sectionCreator', [
307
            ['module' => 'section', 'function' => 'edit'],
308
        ]);
309
        $user = $this->createCustomUserWithLogin(
310
            'user',
311
            '[email protected]',
312
            'sectionCreators',
313
            'sectionCreator',
314
            new SectionLimitation(['limitationValues' => [$section->id]])
315
        );
316
        $this->permissionResolver->setCurrentUserReference($user);
317
318
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
319
        /* END: Use Case */
320
321
        // Verify that service returns an instance of Section
322
        $this->assertInstanceOf(Section::class, $updatedSection);
323
324
        // Load section as an administrator
325
        $administratorUser = $userService->loadUser($administratorUserId);
326
        $this->permissionResolver->setCurrentUserReference($administratorUser);
327
328
        // Verify that the service also persists the changes
329
        $updatedSection = $sectionService->loadSection($section->id);
330
331
        $this->assertEquals('New section name', $updatedSection->name);
332
        $this->assertEquals('newUniqueKey', $updatedSection->identifier);
333
    }
334
335
    /**
336
     * Test for the updateSection() method.
337
     *
338
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
339
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
340
     */
341
    public function testUpdateSectionKeepsSectionIdentifierOnNameUpdate()
342
    {
343
        $repository = $this->getRepository();
344
345
        $standardSectionId = $this->generateId('section', 1);
346
        /* BEGIN: Use Case */
347
        // $standardSectionId contains the ID of the "Standard" section in a eZ
348
        // Publish demo installation.
349
350
        $sectionService = $repository->getSectionService();
351
352
        $section = $sectionService->loadSection($standardSectionId);
353
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
354
        $sectionUpdate->name = 'New section name';
355
356
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
357
        /* END: Use Case */
358
359
        $this->assertEquals('standard', $updatedSection->identifier);
360
    }
361
362
    /**
363
     * Test for the updateSection() method.
364
     *
365
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
366
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
367
     */
368
    public function testUpdateSectionWithSectionIdentifierOnNameUpdate()
369
    {
370
        $repository = $this->getRepository();
371
372
        $standardSectionId = $this->generateId('section', 1);
373
        /* BEGIN: Use Case */
374
        // $standardSectionId contains the ID of the "Standard" section in a eZ
375
        // Publish demo installation.
376
377
        $sectionService = $repository->getSectionService();
378
379
        $section = $sectionService->loadSection($standardSectionId);
380
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
381
        $sectionUpdate->name = 'New section name';
382
383
        // section identifier remains the same
384
        $sectionUpdate->identifier = $section->identifier;
385
386
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
387
        /* END: Use Case */
388
389
        $this->assertEquals('standard', $updatedSection->identifier);
390
    }
391
392
    /**
393
     * Test for the updateSection() method.
394
     *
395
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
396
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
397
     */
398
    public function testUpdateSectionKeepsSectionNameOnIdentifierUpdate()
399
    {
400
        $repository = $this->getRepository();
401
402
        $standardSectionId = $this->generateId('section', 1);
403
        /* BEGIN: Use Case */
404
        // $standardSectionId contains the ID of the "Standard" section in a eZ
405
        // Publish demo installation.
406
407
        $sectionService = $repository->getSectionService();
408
409
        $section = $sectionService->loadSection($standardSectionId);
410
411
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
412
        $sectionUpdate->identifier = 'newUniqueKey';
413
414
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
415
        /* END: Use Case */
416
417
        $this->assertEquals('Standard', $updatedSection->name);
418
    }
419
420
    /**
421
     * Test for the updateSection() method.
422
     *
423
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
424
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
425
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
426
     */
427
    public function testUpdateSectionThrowsInvalidArgumentException()
428
    {
429
        $repository = $this->getRepository();
430
431
        $standardSectionId = $this->generateId('section', 1);
432
        /* BEGIN: Use Case */
433
        // $standardSectionId contains the ID of the "Standard" section in a eZ
434
        // Publish demo installation.
435
436
        $sectionService = $repository->getSectionService();
437
438
        // Create section with conflict identifier
439
        $sectionCreate = $sectionService->newSectionCreateStruct();
440
        $sectionCreate->name = 'Conflict section';
441
        $sectionCreate->identifier = 'conflictKey';
442
443
        $sectionService->createSection($sectionCreate);
444
445
        // Load an existing section and update to an existing identifier
446
        $section = $sectionService->loadSection($standardSectionId);
447
448
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
449
        $sectionUpdate->identifier = 'conflictKey';
450
451
        // This call should fail with an InvalidArgumentException
452
        $sectionService->updateSection($section, $sectionUpdate);
453
        /* END: Use Case */
454
    }
455
456
    /**
457
     * Test for the loadSections() method.
458
     *
459
     * @see \eZ\Publish\API\Repository\SectionService::loadSections()
460
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
461
     */
462
    public function testLoadSections()
463
    {
464
        $repository = $this->getRepository();
465
466
        /* BEGIN: Use Case */
467
        $sectionService = $repository->getSectionService();
468
469
        $sections = $sectionService->loadSections();
470
        foreach ($sections as $section) {
471
            // Operate on all sections.
472
        }
473
        /* END: Use Case */
474
475
        $this->assertEquals(6, count($sections));
476
    }
477
478
    /**
479
     * Test for the loadSections() method.
480
     *
481
     * @see \eZ\Publish\API\Repository\SectionService::loadSections()
482
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
483
     */
484
    public function testLoadSectionsReturnsDefaultSectionsByDefault()
485
    {
486
        $repository = $this->getRepository();
487
488
        $sectionService = $repository->getSectionService();
489
490
        $this->assertEquals(
491
            [
492
                new Section(
493
                    [
494
                        'id' => $this->generateId('section', 1),
495
                        'name' => 'Standard',
496
                        'identifier' => 'standard',
497
                    ]
498
                ),
499
                new Section(
500
                    [
501
                        'id' => $this->generateId('section', 2),
502
                        'name' => 'Users',
503
                        'identifier' => 'users',
504
                    ]
505
                ),
506
                new Section(
507
                    [
508
                        'id' => $this->generateId('section', 3),
509
                        'name' => 'Media',
510
                        'identifier' => 'media',
511
                    ]
512
                ),
513
                new Section(
514
                    [
515
                        'id' => $this->generateId('section', 4),
516
                        'name' => 'Setup',
517
                        'identifier' => 'setup',
518
                    ]
519
                ),
520
                new Section(
521
                    [
522
                        'id' => $this->generateId('section', 5),
523
                        'name' => 'Design',
524
                        'identifier' => 'design',
525
                    ]
526
                ),
527
                new Section(
528
                    [
529
                        'id' => $this->generateId('section', 6),
530
                        'name' => 'Restricted',
531
                        'identifier' => '',
532
                    ]
533
                ),
534
            ],
535
            $sectionService->loadSections()
536
        );
537
    }
538
539
    /**
540
     * Test for the loadSectionByIdentifier() method.
541
     *
542
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
543
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
544
     */
545
    public function testLoadSectionByIdentifier()
546
    {
547
        $repository = $this->getRepository();
548
549
        /* BEGIN: Use Case */
550
        $sectionService = $repository->getSectionService();
551
552
        $sectionCreate = $sectionService->newSectionCreateStruct();
553
        $sectionCreate->name = 'Test Section';
554
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
555
556
        $sectionId = $sectionService->createSection($sectionCreate)->id;
557
558
        $section = $sectionService->loadSectionByIdentifier(self::SECTION_UNIQUE_KEY);
559
        /* END: Use Case */
560
561
        $this->assertEquals($sectionId, $section->id);
562
    }
563
564
    /**
565
     * Test for the loadSectionByIdentifier() method.
566
     *
567
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
568
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
569
     */
570
    public function testLoadSectionByIdentifierThrowsNotFoundException()
571
    {
572
        $repository = $this->getRepository();
573
574
        /* BEGIN: Use Case */
575
        $sectionService = $repository->getSectionService();
576
577
        // This call should fail with a NotFoundException
578
        $sectionService->loadSectionByIdentifier('someUnknownSectionIdentifier');
579
        /* END: Use Case */
580
    }
581
582
    /**
583
     * Test for the countAssignedContents() method.
584
     *
585
     * @see \eZ\Publish\API\Repository\SectionService::countAssignedContents()
586
     */
587
    public function testCountAssignedContents()
588
    {
589
        $repository = $this->getRepository();
590
591
        $sectionService = $repository->getSectionService();
592
593
        $standardSectionId = $this->generateId('section', 1);
594
        /* BEGIN: Use Case */
595
        // $standardSectionId contains the ID of the "Standard" section in a eZ
596
        // Publish demo installation.
597
598
        $standardSection = $sectionService->loadSection($standardSectionId);
599
600
        $numberOfAssignedContent = $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
601
            $standardSection
602
        );
603
        /* END: Use Case */
604
605
        $this->assertEquals(
606
            2, // Taken from the fixture
607
            $numberOfAssignedContent
608
        );
609
    }
610
611
    /**
612
     * Test for the isSectionUsed() method.
613
     *
614
     * @see \eZ\Publish\API\Repository\SectionService::isSectionUsed()
615
     */
616
    public function testIsSectionUsed()
617
    {
618
        $repository = $this->getRepository();
619
620
        $sectionService = $repository->getSectionService();
621
622
        $standardSectionId = $this->generateId('section', 1);
623
        /* BEGIN: Use Case */
624
        // $standardSectionId contains the ID of the "Standard" section in a eZ
625
        // Publish demo installation.
626
627
        $standardSection = $sectionService->loadSection($standardSectionId);
628
629
        $isSectionUsed = $sectionService->isSectionUsed(
630
            $standardSection
631
        );
632
        /* END: Use Case */
633
634
        $this->assertTrue(
635
            // Taken from the fixture
636
            $isSectionUsed
637
        );
638
    }
639
640
    /**
641
     * Test for the assignSection() method.
642
     *
643
     * @see \eZ\Publish\API\Repository\SectionService::assignSection()
644
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCountAssignedContents
645
     */
646
    public function testAssignSection()
647
    {
648
        $repository = $this->getRepository();
649
        $sectionService = $repository->getSectionService();
650
651
        $standardSectionId = $this->generateId('section', 1);
652
        $mediaSectionId = $this->generateId('section', 3);
653
654
        $beforeStandardCount = $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
655
            $sectionService->loadSection($standardSectionId)
656
        );
657
        $beforeMediaCount = $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
658
            $sectionService->loadSection($mediaSectionId)
659
        );
660
661
        /* BEGIN: Use Case */
662
        // $mediaSectionId contains the ID of the "Media" section in a eZ
663
        // Publish demo installation.
664
665
        // RemoteId of the "Media" page of an eZ Publish demo installation
666
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
667
668
        $contentService = $repository->getContentService();
669
        $sectionService = $repository->getSectionService();
670
671
        // Load a content info instance
672
        $contentInfo = $contentService->loadContentInfoByRemoteId(
673
            $mediaRemoteId
674
        );
675
676
        // Load the "Standard" section
677
        $section = $sectionService->loadSection($standardSectionId);
678
679
        // Assign Section to ContentInfo
680
        $sectionService->assignSection($contentInfo, $section);
681
        /* END: Use Case */
682
683
        $this->assertEquals(
684
            $beforeStandardCount + 1,
685
            $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
686
                $sectionService->loadSection($standardSectionId)
687
            )
688
        );
689
        $this->assertEquals(
690
            $beforeMediaCount - 1,
691
            $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
692
                $sectionService->loadSection($mediaSectionId)
693
            )
694
        );
695
    }
696
697
    /**
698
     * Test for the assignSectionToSubtree() method.
699
     *
700
     * @see \eZ\Publish\API\Repository\SectionService::assignSectionToSubtree()
701
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
702
     */
703
    public function testAssignSectionToSubtree()
704
    {
705
        $repository = $this->getRepository();
706
        $sectionService = $repository->getSectionService();
707
708
        $standardSectionId = $this->generateId('section', 1);
709
        $mediaSectionId = $this->generateId('section', 3);
710
711
        $beforeStandardCount = $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
712
            $sectionService->loadSection($standardSectionId)
713
        );
714
715
        $beforeMediaCount = $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
716
            $sectionService->loadSection($mediaSectionId)
717
        );
718
719
        // RemoteId of the "Media" page of an eZ Publish demo installation
720
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
721
722
        /* BEGIN: Use Case */
723
        $locationService = $repository->getLocationService();
724
725
        // Load a location instance
726
        $location = $locationService->loadLocationByRemoteId($mediaRemoteId);
727
728
        // Load the "Standard" section
729
        $section = $sectionService->loadSection($standardSectionId);
730
731
        // Assign Section to ContentInfo
732
        $sectionService->assignSectionToSubtree($location, $section);
733
734
        /* END: Use Case */
735
        $this->assertEquals(
736
            $beforeStandardCount + 4,
737
            $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
738
                $sectionService->loadSection($standardSectionId)
739
            )
740
        );
741
        $this->assertEquals(
742
            $beforeMediaCount - 4,
743
            $sectionService->countAssignedContents(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
744
                $sectionService->loadSection($mediaSectionId)
745
            )
746
        );
747
    }
748
749
    /**
750
     * Test for the countAssignedContents() method.
751
     *
752
     * @see \eZ\Publish\API\Repository\SectionService::countAssignedContents()
753
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
754
     */
755
    public function testCountAssignedContentsReturnsZeroByDefault()
756
    {
757
        $repository = $this->getRepository();
758
759
        /* BEGIN: Use Case */
760
        $sectionService = $repository->getSectionService();
761
762
        $sectionCreate = $sectionService->newSectionCreateStruct();
763
        $sectionCreate->name = 'Test Section';
764
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
765
766
        $section = $sectionService->createSection($sectionCreate);
767
768
        // The number of assigned contents should be zero
769
        $assignedContents = $sectionService->countAssignedContents($section);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...countAssignedContents() has been deprecated with message: since 6.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
770
        /* END: Use Case */
771
772
        $this->assertSame(0, $assignedContents);
773
    }
774
775
    /**
776
     * Test for the isSectionUsed() method.
777
     *
778
     * @see \eZ\Publish\API\Repository\SectionService::isSectionUsed()
779
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
780
     */
781
    public function testIsSectionUsedReturnsZeroByDefault()
782
    {
783
        $repository = $this->getRepository();
784
785
        /* BEGIN: Use Case */
786
        $sectionService = $repository->getSectionService();
787
788
        $sectionCreate = $sectionService->newSectionCreateStruct();
789
        $sectionCreate->name = 'Test Section';
790
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
791
792
        $section = $sectionService->createSection($sectionCreate);
793
794
        // The number of assigned contents should be zero
795
        $isSectionUsed = $sectionService->isSectionUsed($section);
796
        /* END: Use Case */
797
798
        $this->assertFalse($isSectionUsed);
799
    }
800
801
    /**
802
     * Test for the deleteSection() method.
803
     *
804
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
805
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSections
806
     */
807
    public function testDeleteSection()
808
    {
809
        $repository = $this->getRepository();
810
811
        /* BEGIN: Use Case */
812
        $sectionService = $repository->getSectionService();
813
814
        $sectionCreate = $sectionService->newSectionCreateStruct();
815
        $sectionCreate->name = 'Test Section';
816
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
817
818
        $section = $sectionService->createSection($sectionCreate);
819
820
        // Delete the newly created section
821
        $sectionService->deleteSection($section);
822
        /* END: Use Case */
823
824
        $this->assertEquals(6, count($sectionService->loadSections()));
825
    }
826
827
    /**
828
     * Test for the deleteSection() method.
829
     *
830
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
831
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
832
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testDeleteSection
833
     */
834
    public function testDeleteSectionThrowsNotFoundException()
835
    {
836
        $repository = $this->getRepository();
837
838
        /* BEGIN: Use Case */
839
        $sectionService = $repository->getSectionService();
840
841
        $sectionCreate = $sectionService->newSectionCreateStruct();
842
        $sectionCreate->name = 'Test Section';
843
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
844
845
        $section = $sectionService->createSection($sectionCreate);
846
847
        // Delete the newly created section
848
        $sectionService->deleteSection($section);
849
850
        // This call should fail with a NotFoundException
851
        $sectionService->deleteSection($section);
852
        /* END: Use Case */
853
    }
854
855
    /**
856
     * Test for the deleteSection() method.
857
     *
858
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
859
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
860
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testAssignSection
861
     */
862
    public function testDeleteSectionThrowsBadStateException()
863
    {
864
        $repository = $this->getRepository();
865
866
        $standardSectionId = $this->generateId('section', 1);
867
        /* BEGIN: Use Case */
868
        // $standardSectionId contains the ID of the "Standard" section in a eZ
869
        // Publish demo installation.
870
871
        // RemoteId of the "Media" page of an eZ Publish demo installation
872
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
873
874
        $contentService = $repository->getContentService();
875
        $sectionService = $repository->getSectionService();
876
877
        // Load the "Media" ContentInfo
878
        $contentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
879
880
        // Load the "Standard" section
881
        $section = $sectionService->loadSection($standardSectionId);
882
883
        // Assign "Media" to "Standard" section
884
        $sectionService->assignSection($contentInfo, $section);
885
886
        // This call should fail with a BadStateException, because there are assigned contents
887
        $sectionService->deleteSection($section);
888
        /* END: Use Case */
889
    }
890
891
    /**
892
     * Test for the createSection() method.
893
     *
894
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
895
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
896
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
897
     */
898
    public function testCreateSectionInTransactionWithRollback()
899
    {
900
        $repository = $this->getRepository();
901
902
        /* BEGIN: Use Case */
903
        $sectionService = $repository->getSectionService();
904
905
        // Start a new transaction
906
        $repository->beginTransaction();
907
908
        try {
909
            // Get a create struct and set some properties
910
            $sectionCreate = $sectionService->newSectionCreateStruct();
911
            $sectionCreate->name = 'Test Section';
912
            $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
913
914
            // Create a new section
915
            $sectionService->createSection($sectionCreate);
916
        } catch (Exception $e) {
917
            // Cleanup hanging transaction on error
918
            $repository->rollback();
919
            throw $e;
920
        }
921
922
        // Rollback all changes
923
        $repository->rollback();
924
925
        try {
926
            // This call will fail with a not found exception
927
            $sectionService->loadSectionByIdentifier(self::SECTION_UNIQUE_KEY);
928
        } catch (NotFoundException $e) {
929
            // Expected execution path
930
        }
931
        /* END: Use Case */
932
933
        $this->assertTrue(isset($e), 'Can still load section after rollback.');
934
    }
935
936
    /**
937
     * Test for the createSection() method.
938
     *
939
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
940
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
941
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
942
     */
943
    public function testCreateSectionInTransactionWithCommit()
944
    {
945
        $repository = $this->getRepository();
946
947
        /* BEGIN: Use Case */
948
        $sectionService = $repository->getSectionService();
949
950
        // Start a new transaction
951
        $repository->beginTransaction();
952
953
        try {
954
            // Get a create struct and set some properties
955
            $sectionCreate = $sectionService->newSectionCreateStruct();
956
            $sectionCreate->name = 'Test Section';
957
            $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
958
959
            // Create a new section
960
            $sectionService->createSection($sectionCreate);
961
962
            // Commit all changes
963
            $repository->commit();
964
        } catch (Exception $e) {
965
            // Cleanup hanging transaction on error
966
            $repository->rollback();
967
            throw $e;
968
        }
969
970
        // Load new section
971
        $section = $sectionService->loadSectionByIdentifier(self::SECTION_UNIQUE_KEY);
972
        /* END: Use Case */
973
974
        $this->assertEquals(self::SECTION_UNIQUE_KEY, $section->identifier);
975
    }
976
977
    /**
978
     * Test for the createSection() method.
979
     *
980
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
981
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
982
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
983
     */
984
    public function testUpdateSectionInTransactionWithRollback()
985
    {
986
        $repository = $this->getRepository();
987
988
        /* BEGIN: Use Case */
989
        $sectionService = $repository->getSectionService();
990
991
        // Start a new transaction
992
        $repository->beginTransaction();
993
994
        try {
995
            // Load standard section
996
            $section = $sectionService->loadSectionByIdentifier('standard');
997
998
            // Get an update struct and change section name
999
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
1000
            $sectionUpdate->name = 'My Standard';
1001
1002
            // Update section
1003
            $sectionService->updateSection($section, $sectionUpdate);
1004
        } catch (Exception $e) {
1005
            // Cleanup hanging transaction on error
1006
            $repository->rollback();
1007
            throw $e;
1008
        }
1009
1010
        // Rollback all changes
1011
        $repository->rollback();
1012
1013
        // Load updated section, name will still be "Standard"
1014
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
1015
        /* END: Use Case */
1016
1017
        $this->assertEquals('Standard', $updatedStandard->name);
1018
    }
1019
1020
    /**
1021
     * Test for the createSection() method.
1022
     *
1023
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
1024
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
1025
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
1026
     */
1027
    public function testUpdateSectionInTransactionWithCommit()
1028
    {
1029
        $repository = $this->getRepository();
1030
1031
        /* BEGIN: Use Case */
1032
        $sectionService = $repository->getSectionService();
1033
1034
        // Start a new transaction
1035
        $repository->beginTransaction();
1036
1037
        try {
1038
            // Load standard section
1039
            $section = $sectionService->loadSectionByIdentifier('standard');
1040
1041
            // Get an update struct and change section name
1042
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
1043
            $sectionUpdate->name = 'My Standard';
1044
1045
            // Update section
1046
            $sectionService->updateSection($section, $sectionUpdate);
1047
1048
            // Commit all changes
1049
            $repository->commit();
1050
        } catch (Exception $e) {
1051
            // Cleanup hanging transaction on error
1052
            $repository->rollback();
1053
            throw $e;
1054
        }
1055
1056
        // Load updated section, name will now be "My Standard"
1057
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
1058
        /* END: Use Case */
1059
1060
        $this->assertEquals('My Standard', $updatedStandard->name);
1061
    }
1062
}
1063