Completed
Push — ezp-30882-thumbnail ( 274ed9...d4335b )
by
unknown
14:43
created

SectionServiceTest   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 1044
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 0
loc 1044
rs 8.704
c 0
b 0
f 0
wmc 39
lcom 1
cbo 11

31 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 35 3
A testNewSectionCreateStruct() 0 12 1
A testCreateSection() 0 16 1
A testCreateSectionForUserWithSectionLimitation() 0 31 1
A testCreateSectionThrowsInvalidArgumentException() 0 23 1
A testLoadSection() 0 15 1
A testLoadSectionThrowsNotFoundException() 0 15 1
A testNewSectionUpdateStruct() 0 12 1
A testUpdateSection() 0 28 1
A testUpdateSectionForUserWithSectionLimitation() 0 48 1
A testUpdateSectionKeepsSectionIdentifierOnNameUpdate() 0 20 1
A testUpdateSectionWithSectionIdentifierOnNameUpdate() 0 23 1
A testUpdateSectionKeepsSectionNameOnIdentifierUpdate() 0 21 1
A testUpdateSectionThrowsInvalidArgumentException() 0 30 1
A testLoadSections() 0 15 2
A testLoadSectionsReturnsDefaultSectionsByDefault() 0 54 1
A testLoadSectionByIdentifier() 0 18 1
A testLoadSectionByIdentifierThrowsNotFoundException() 0 13 1
A testCountAssignedContents() 0 23 1
A testIsSectionUsed() 0 23 1
A testAssignSection() 0 50 1
A testAssignSectionToSubtree() 0 45 1
A testCountAssignedContentsReturnsZeroByDefault() 0 19 1
A testIsSectionUsedReturnsZeroByDefault() 0 19 1
A testDeleteSection() 0 19 1
A testDeleteSectionThrowsNotFoundException() 0 22 1
A testDeleteSectionThrowsBadStateException() 0 30 1
A testCreateSectionInTransactionWithRollback() 0 37 3
A testCreateSectionInTransactionWithCommit() 0 33 2
A testUpdateSectionInTransactionWithRollback() 0 35 2
A testUpdateSectionInTransactionWithCommit() 0 35 2
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
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
157
     */
158
    public function testCreateSectionThrowsInvalidArgumentException()
159
    {
160
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
161
162
        $repository = $this->getRepository();
163
164
        /* BEGIN: Use Case */
165
        $sectionService = $repository->getSectionService();
166
167
        $sectionCreateOne = $sectionService->newSectionCreateStruct();
168
        $sectionCreateOne->name = 'Test section one';
169
        $sectionCreateOne->identifier = self::SECTION_UNIQUE_KEY;
170
171
        $sectionService->createSection($sectionCreateOne);
172
173
        $sectionCreateTwo = $sectionService->newSectionCreateStruct();
174
        $sectionCreateTwo->name = 'Test section two';
175
        $sectionCreateTwo->identifier = self::SECTION_UNIQUE_KEY;
176
177
        // This will fail, because identifier uniqueKey already exists.
178
        $sectionService->createSection($sectionCreateTwo);
179
        /* END: Use Case */
180
    }
181
182
    /**
183
     * Test for the loadSection() method.
184
     *
185
     * @see \eZ\Publish\API\Repository\SectionService::loadSection()
186
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
187
     */
188
    public function testLoadSection()
189
    {
190
        $repository = $this->getRepository();
191
192
        $sectionId = $this->generateId('section', 2);
193
        /* BEGIN: Use Case */
194
        $sectionService = $repository->getSectionService();
195
196
        // Loads user section
197
        // $sectionId contains the corresponding ID
198
        $section = $sectionService->loadSection($sectionId);
199
        /* END: Use Case */
200
201
        $this->assertEquals('users', $section->identifier);
202
    }
203
204
    /**
205
     * Test for the loadSection() method.
206
     *
207
     * @see \eZ\Publish\API\Repository\SectionService::loadSection()
208
     */
209
    public function testLoadSectionThrowsNotFoundException()
210
    {
211
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
212
213
        $repository = $this->getRepository();
214
215
        $nonExistentSectionId = $this->generateId('section', self::DB_INT_MAX);
216
        /* BEGIN: Use Case */
217
        $sectionService = $repository->getSectionService();
218
219
        // This call should fail with a NotFoundException
220
        // $nonExistentSectionId contains a section ID that is not known
221
        $sectionService->loadSection($nonExistentSectionId);
222
        /* END: Use Case */
223
    }
224
225
    /**
226
     * Test for the newSectionUpdateStruct() method.
227
     *
228
     * @see \eZ\Publish\API\Repository\SectionService::newSectionUpdateStruct()
229
     */
230
    public function testNewSectionUpdateStruct()
231
    {
232
        $repository = $this->getRepository();
233
234
        /* BEGIN: Use Case */
235
        $sectionService = $repository->getSectionService();
236
237
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
238
        /* END: Use Case */
239
240
        $this->assertInstanceOf(SectionUpdateStruct::class, $sectionUpdate);
241
    }
242
243
    /**
244
     * Test for the updateSection() method.
245
     *
246
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
247
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
248
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
249
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionUpdateStruct
250
     */
251
    public function testUpdateSection()
252
    {
253
        $repository = $this->getRepository();
254
255
        $standardSectionId = $this->generateId('section', 1);
256
        /* BEGIN: Use Case */
257
        // $standardSectionId contains the ID of the "Standard" section in a eZ
258
        // Publish demo installation.
259
260
        $sectionService = $repository->getSectionService();
261
262
        $section = $sectionService->loadSection($standardSectionId);
263
264
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
265
        $sectionUpdate->name = 'New section name';
266
        $sectionUpdate->identifier = 'newUniqueKey';
267
268
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
269
        /* END: Use Case */
270
271
        // Verify that service returns an instance of Section
272
        $this->assertInstanceOf(Section::class, $updatedSection);
273
274
        // Verify that the service also persists the changes
275
        $updatedSection = $sectionService->loadSection($standardSectionId);
276
277
        $this->assertEquals('New section name', $updatedSection->name);
278
    }
279
280
    /**
281
     * Test for the updateSection() method.
282
     *
283
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
284
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
285
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
286
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionUpdateStruct
287
     */
288
    public function testUpdateSectionForUserWithSectionLimitation()
289
    {
290
        $repository = $this->getRepository();
291
        $administratorUserId = $this->generateId('user', 14);
292
        /* BEGIN: Use Case */
293
        // $standardSectionId contains the ID of the "Standard" section in a eZ
294
        // Publish demo installation.
295
296
        $sectionService = $repository->getSectionService();
297
        $userService = $repository->getUserService();
298
299
        $sectionCreate = $sectionService->newSectionCreateStruct();
300
        $sectionCreate->name = 'Test Section';
301
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
302
        $section = $sectionService->createSection($sectionCreate);
303
304
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
305
        $sectionUpdate->name = 'New section name';
306
        $sectionUpdate->identifier = 'newUniqueKey';
307
308
        $this->createRoleWithPolicies('sectionCreator', [
309
            ['module' => 'section', 'function' => 'edit'],
310
        ]);
311
        $user = $this->createCustomUserWithLogin(
312
            'user',
313
            '[email protected]',
314
            'sectionCreators',
315
            'sectionCreator',
316
            new SectionLimitation(['limitationValues' => [$section->id]])
317
        );
318
        $this->permissionResolver->setCurrentUserReference($user);
319
320
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
321
        /* END: Use Case */
322
323
        // Verify that service returns an instance of Section
324
        $this->assertInstanceOf(Section::class, $updatedSection);
325
326
        // Load section as an administrator
327
        $administratorUser = $userService->loadUser($administratorUserId);
328
        $this->permissionResolver->setCurrentUserReference($administratorUser);
329
330
        // Verify that the service also persists the changes
331
        $updatedSection = $sectionService->loadSection($section->id);
332
333
        $this->assertEquals('New section name', $updatedSection->name);
334
        $this->assertEquals('newUniqueKey', $updatedSection->identifier);
335
    }
336
337
    /**
338
     * Test for the updateSection() method.
339
     *
340
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
341
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
342
     */
343
    public function testUpdateSectionKeepsSectionIdentifierOnNameUpdate()
344
    {
345
        $repository = $this->getRepository();
346
347
        $standardSectionId = $this->generateId('section', 1);
348
        /* BEGIN: Use Case */
349
        // $standardSectionId contains the ID of the "Standard" section in a eZ
350
        // Publish demo installation.
351
352
        $sectionService = $repository->getSectionService();
353
354
        $section = $sectionService->loadSection($standardSectionId);
355
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
356
        $sectionUpdate->name = 'New section name';
357
358
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
359
        /* END: Use Case */
360
361
        $this->assertEquals('standard', $updatedSection->identifier);
362
    }
363
364
    /**
365
     * Test for the updateSection() method.
366
     *
367
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
368
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
369
     */
370
    public function testUpdateSectionWithSectionIdentifierOnNameUpdate()
371
    {
372
        $repository = $this->getRepository();
373
374
        $standardSectionId = $this->generateId('section', 1);
375
        /* BEGIN: Use Case */
376
        // $standardSectionId contains the ID of the "Standard" section in a eZ
377
        // Publish demo installation.
378
379
        $sectionService = $repository->getSectionService();
380
381
        $section = $sectionService->loadSection($standardSectionId);
382
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
383
        $sectionUpdate->name = 'New section name';
384
385
        // section identifier remains the same
386
        $sectionUpdate->identifier = $section->identifier;
387
388
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
389
        /* END: Use Case */
390
391
        $this->assertEquals('standard', $updatedSection->identifier);
392
    }
393
394
    /**
395
     * Test for the updateSection() method.
396
     *
397
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
398
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
399
     */
400
    public function testUpdateSectionKeepsSectionNameOnIdentifierUpdate()
401
    {
402
        $repository = $this->getRepository();
403
404
        $standardSectionId = $this->generateId('section', 1);
405
        /* BEGIN: Use Case */
406
        // $standardSectionId contains the ID of the "Standard" section in a eZ
407
        // Publish demo installation.
408
409
        $sectionService = $repository->getSectionService();
410
411
        $section = $sectionService->loadSection($standardSectionId);
412
413
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
414
        $sectionUpdate->identifier = 'newUniqueKey';
415
416
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
417
        /* END: Use Case */
418
419
        $this->assertEquals('Standard', $updatedSection->name);
420
    }
421
422
    /**
423
     * Test for the updateSection() method.
424
     *
425
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
426
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
427
     */
428
    public function testUpdateSectionThrowsInvalidArgumentException()
429
    {
430
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
431
432
        $repository = $this->getRepository();
433
434
        $standardSectionId = $this->generateId('section', 1);
435
        /* BEGIN: Use Case */
436
        // $standardSectionId contains the ID of the "Standard" section in a eZ
437
        // Publish demo installation.
438
439
        $sectionService = $repository->getSectionService();
440
441
        // Create section with conflict identifier
442
        $sectionCreate = $sectionService->newSectionCreateStruct();
443
        $sectionCreate->name = 'Conflict section';
444
        $sectionCreate->identifier = 'conflictKey';
445
446
        $sectionService->createSection($sectionCreate);
447
448
        // Load an existing section and update to an existing identifier
449
        $section = $sectionService->loadSection($standardSectionId);
450
451
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
452
        $sectionUpdate->identifier = 'conflictKey';
453
454
        // This call should fail with an InvalidArgumentException
455
        $sectionService->updateSection($section, $sectionUpdate);
456
        /* END: Use Case */
457
    }
458
459
    /**
460
     * Test for the loadSections() method.
461
     *
462
     * @see \eZ\Publish\API\Repository\SectionService::loadSections()
463
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
464
     */
465
    public function testLoadSections()
466
    {
467
        $repository = $this->getRepository();
468
469
        /* BEGIN: Use Case */
470
        $sectionService = $repository->getSectionService();
471
472
        $sections = $sectionService->loadSections();
473
        foreach ($sections as $section) {
474
            // Operate on all sections.
475
        }
476
        /* END: Use Case */
477
478
        $this->assertCount(6, $sections);
479
    }
480
481
    /**
482
     * Test for the loadSections() method.
483
     *
484
     * @see \eZ\Publish\API\Repository\SectionService::loadSections()
485
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
486
     */
487
    public function testLoadSectionsReturnsDefaultSectionsByDefault()
488
    {
489
        $repository = $this->getRepository();
490
491
        $sectionService = $repository->getSectionService();
492
493
        $this->assertEquals(
494
            [
495
                new Section(
496
                    [
497
                        'id' => $this->generateId('section', 1),
498
                        'name' => 'Standard',
499
                        'identifier' => 'standard',
500
                    ]
501
                ),
502
                new Section(
503
                    [
504
                        'id' => $this->generateId('section', 2),
505
                        'name' => 'Users',
506
                        'identifier' => 'users',
507
                    ]
508
                ),
509
                new Section(
510
                    [
511
                        'id' => $this->generateId('section', 3),
512
                        'name' => 'Media',
513
                        'identifier' => 'media',
514
                    ]
515
                ),
516
                new Section(
517
                    [
518
                        'id' => $this->generateId('section', 4),
519
                        'name' => 'Setup',
520
                        'identifier' => 'setup',
521
                    ]
522
                ),
523
                new Section(
524
                    [
525
                        'id' => $this->generateId('section', 5),
526
                        'name' => 'Design',
527
                        'identifier' => 'design',
528
                    ]
529
                ),
530
                new Section(
531
                    [
532
                        'id' => $this->generateId('section', 6),
533
                        'name' => 'Restricted',
534
                        'identifier' => '',
535
                    ]
536
                ),
537
            ],
538
            $sectionService->loadSections()
539
        );
540
    }
541
542
    /**
543
     * Test for the loadSectionByIdentifier() method.
544
     *
545
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
546
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
547
     */
548
    public function testLoadSectionByIdentifier()
549
    {
550
        $repository = $this->getRepository();
551
552
        /* BEGIN: Use Case */
553
        $sectionService = $repository->getSectionService();
554
555
        $sectionCreate = $sectionService->newSectionCreateStruct();
556
        $sectionCreate->name = 'Test Section';
557
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
558
559
        $sectionId = $sectionService->createSection($sectionCreate)->id;
560
561
        $section = $sectionService->loadSectionByIdentifier(self::SECTION_UNIQUE_KEY);
562
        /* END: Use Case */
563
564
        $this->assertEquals($sectionId, $section->id);
565
    }
566
567
    /**
568
     * Test for the loadSectionByIdentifier() method.
569
     *
570
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
571
     */
572
    public function testLoadSectionByIdentifierThrowsNotFoundException()
573
    {
574
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
575
576
        $repository = $this->getRepository();
577
578
        /* BEGIN: Use Case */
579
        $sectionService = $repository->getSectionService();
580
581
        // This call should fail with a NotFoundException
582
        $sectionService->loadSectionByIdentifier('someUnknownSectionIdentifier');
583
        /* END: Use Case */
584
    }
585
586
    /**
587
     * Test for the countAssignedContents() method.
588
     *
589
     * @see \eZ\Publish\API\Repository\SectionService::countAssignedContents()
590
     */
591
    public function testCountAssignedContents()
592
    {
593
        $repository = $this->getRepository();
594
595
        $sectionService = $repository->getSectionService();
596
597
        $standardSectionId = $this->generateId('section', 1);
598
        /* BEGIN: Use Case */
599
        // $standardSectionId contains the ID of the "Standard" section in a eZ
600
        // Publish demo installation.
601
602
        $standardSection = $sectionService->loadSection($standardSectionId);
603
604
        $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...
605
            $standardSection
606
        );
607
        /* END: Use Case */
608
609
        $this->assertEquals(
610
            2, // Taken from the fixture
611
            $numberOfAssignedContent
612
        );
613
    }
614
615
    /**
616
     * Test for the isSectionUsed() method.
617
     *
618
     * @see \eZ\Publish\API\Repository\SectionService::isSectionUsed()
619
     */
620
    public function testIsSectionUsed()
621
    {
622
        $repository = $this->getRepository();
623
624
        $sectionService = $repository->getSectionService();
625
626
        $standardSectionId = $this->generateId('section', 1);
627
        /* BEGIN: Use Case */
628
        // $standardSectionId contains the ID of the "Standard" section in a eZ
629
        // Publish demo installation.
630
631
        $standardSection = $sectionService->loadSection($standardSectionId);
632
633
        $isSectionUsed = $sectionService->isSectionUsed(
634
            $standardSection
635
        );
636
        /* END: Use Case */
637
638
        $this->assertTrue(
639
            // Taken from the fixture
640
            $isSectionUsed
641
        );
642
    }
643
644
    /**
645
     * Test for the assignSection() method.
646
     *
647
     * @see \eZ\Publish\API\Repository\SectionService::assignSection()
648
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCountAssignedContents
649
     */
650
    public function testAssignSection()
651
    {
652
        $repository = $this->getRepository();
653
        $sectionService = $repository->getSectionService();
654
655
        $standardSectionId = $this->generateId('section', 1);
656
        $mediaSectionId = $this->generateId('section', 3);
657
658
        $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...
659
            $sectionService->loadSection($standardSectionId)
660
        );
661
        $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...
662
            $sectionService->loadSection($mediaSectionId)
663
        );
664
665
        /* BEGIN: Use Case */
666
        // $mediaSectionId contains the ID of the "Media" section in a eZ
667
        // Publish demo installation.
668
669
        // RemoteId of the "Media" page of an eZ Publish demo installation
670
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
671
672
        $contentService = $repository->getContentService();
673
        $sectionService = $repository->getSectionService();
674
675
        // Load a content info instance
676
        $contentInfo = $contentService->loadContentInfoByRemoteId(
677
            $mediaRemoteId
678
        );
679
680
        // Load the "Standard" section
681
        $section = $sectionService->loadSection($standardSectionId);
682
683
        // Assign Section to ContentInfo
684
        $sectionService->assignSection($contentInfo, $section);
685
        /* END: Use Case */
686
687
        $this->assertEquals(
688
            $beforeStandardCount + 1,
689
            $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...
690
                $sectionService->loadSection($standardSectionId)
691
            )
692
        );
693
        $this->assertEquals(
694
            $beforeMediaCount - 1,
695
            $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...
696
                $sectionService->loadSection($mediaSectionId)
697
            )
698
        );
699
    }
700
701
    /**
702
     * Test for the assignSectionToSubtree() method.
703
     *
704
     * @see \eZ\Publish\API\Repository\SectionService::assignSectionToSubtree()
705
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
706
     */
707
    public function testAssignSectionToSubtree()
708
    {
709
        $repository = $this->getRepository();
710
        $sectionService = $repository->getSectionService();
711
712
        $standardSectionId = $this->generateId('section', 1);
713
        $mediaSectionId = $this->generateId('section', 3);
714
715
        $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...
716
            $sectionService->loadSection($standardSectionId)
717
        );
718
719
        $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...
720
            $sectionService->loadSection($mediaSectionId)
721
        );
722
723
        // RemoteId of the "Media" page of an eZ Publish demo installation
724
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
725
726
        /* BEGIN: Use Case */
727
        $locationService = $repository->getLocationService();
728
729
        // Load a location instance
730
        $location = $locationService->loadLocationByRemoteId($mediaRemoteId);
731
732
        // Load the "Standard" section
733
        $section = $sectionService->loadSection($standardSectionId);
734
735
        // Assign Section to ContentInfo
736
        $sectionService->assignSectionToSubtree($location, $section);
737
738
        /* END: Use Case */
739
        $this->assertEquals(
740
            $beforeStandardCount + 4,
741
            $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...
742
                $sectionService->loadSection($standardSectionId)
743
            )
744
        );
745
        $this->assertEquals(
746
            $beforeMediaCount - 4,
747
            $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...
748
                $sectionService->loadSection($mediaSectionId)
749
            )
750
        );
751
    }
752
753
    /**
754
     * Test for the countAssignedContents() method.
755
     *
756
     * @see \eZ\Publish\API\Repository\SectionService::countAssignedContents()
757
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
758
     */
759
    public function testCountAssignedContentsReturnsZeroByDefault()
760
    {
761
        $repository = $this->getRepository();
762
763
        /* BEGIN: Use Case */
764
        $sectionService = $repository->getSectionService();
765
766
        $sectionCreate = $sectionService->newSectionCreateStruct();
767
        $sectionCreate->name = 'Test Section';
768
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
769
770
        $section = $sectionService->createSection($sectionCreate);
771
772
        // The number of assigned contents should be zero
773
        $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...
774
        /* END: Use Case */
775
776
        $this->assertSame(0, $assignedContents);
777
    }
778
779
    /**
780
     * Test for the isSectionUsed() method.
781
     *
782
     * @see \eZ\Publish\API\Repository\SectionService::isSectionUsed()
783
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
784
     */
785
    public function testIsSectionUsedReturnsZeroByDefault()
786
    {
787
        $repository = $this->getRepository();
788
789
        /* BEGIN: Use Case */
790
        $sectionService = $repository->getSectionService();
791
792
        $sectionCreate = $sectionService->newSectionCreateStruct();
793
        $sectionCreate->name = 'Test Section';
794
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
795
796
        $section = $sectionService->createSection($sectionCreate);
797
798
        // The number of assigned contents should be zero
799
        $isSectionUsed = $sectionService->isSectionUsed($section);
800
        /* END: Use Case */
801
802
        $this->assertFalse($isSectionUsed);
803
    }
804
805
    /**
806
     * Test for the deleteSection() method.
807
     *
808
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
809
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSections
810
     */
811
    public function testDeleteSection()
812
    {
813
        $repository = $this->getRepository();
814
815
        /* BEGIN: Use Case */
816
        $sectionService = $repository->getSectionService();
817
818
        $sectionCreate = $sectionService->newSectionCreateStruct();
819
        $sectionCreate->name = 'Test Section';
820
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
821
822
        $section = $sectionService->createSection($sectionCreate);
823
824
        // Delete the newly created section
825
        $sectionService->deleteSection($section);
826
        /* END: Use Case */
827
828
        $this->assertCount(6, $sectionService->loadSections());
829
    }
830
831
    /**
832
     * Test for the deleteSection() method.
833
     *
834
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
835
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testDeleteSection
836
     */
837
    public function testDeleteSectionThrowsNotFoundException()
838
    {
839
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
840
841
        $repository = $this->getRepository();
842
843
        /* BEGIN: Use Case */
844
        $sectionService = $repository->getSectionService();
845
846
        $sectionCreate = $sectionService->newSectionCreateStruct();
847
        $sectionCreate->name = 'Test Section';
848
        $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
849
850
        $section = $sectionService->createSection($sectionCreate);
851
852
        // Delete the newly created section
853
        $sectionService->deleteSection($section);
854
855
        // This call should fail with a NotFoundException
856
        $sectionService->deleteSection($section);
857
        /* END: Use Case */
858
    }
859
860
    /**
861
     * Test for the deleteSection() method.
862
     *
863
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
864
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testAssignSection
865
     */
866
    public function testDeleteSectionThrowsBadStateException()
867
    {
868
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\BadStateException::class);
869
870
        $repository = $this->getRepository();
871
872
        $standardSectionId = $this->generateId('section', 1);
873
        /* BEGIN: Use Case */
874
        // $standardSectionId contains the ID of the "Standard" section in a eZ
875
        // Publish demo installation.
876
877
        // RemoteId of the "Media" page of an eZ Publish demo installation
878
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
879
880
        $contentService = $repository->getContentService();
881
        $sectionService = $repository->getSectionService();
882
883
        // Load the "Media" ContentInfo
884
        $contentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
885
886
        // Load the "Standard" section
887
        $section = $sectionService->loadSection($standardSectionId);
888
889
        // Assign "Media" to "Standard" section
890
        $sectionService->assignSection($contentInfo, $section);
891
892
        // This call should fail with a BadStateException, because there are assigned contents
893
        $sectionService->deleteSection($section);
894
        /* END: Use Case */
895
    }
896
897
    /**
898
     * Test for the createSection() method.
899
     *
900
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
901
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
902
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
903
     */
904
    public function testCreateSectionInTransactionWithRollback()
905
    {
906
        $repository = $this->getRepository();
907
908
        /* BEGIN: Use Case */
909
        $sectionService = $repository->getSectionService();
910
911
        // Start a new transaction
912
        $repository->beginTransaction();
913
914
        try {
915
            // Get a create struct and set some properties
916
            $sectionCreate = $sectionService->newSectionCreateStruct();
917
            $sectionCreate->name = 'Test Section';
918
            $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
919
920
            // Create a new section
921
            $sectionService->createSection($sectionCreate);
922
        } catch (Exception $e) {
923
            // Cleanup hanging transaction on error
924
            $repository->rollback();
925
            throw $e;
926
        }
927
928
        // Rollback all changes
929
        $repository->rollback();
930
931
        try {
932
            // This call will fail with a not found exception
933
            $sectionService->loadSectionByIdentifier(self::SECTION_UNIQUE_KEY);
934
        } catch (NotFoundException $e) {
935
            // Expected execution path
936
        }
937
        /* END: Use Case */
938
939
        $this->assertTrue(isset($e), 'Can still load section after rollback.');
940
    }
941
942
    /**
943
     * Test for the createSection() method.
944
     *
945
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
946
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
947
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
948
     */
949
    public function testCreateSectionInTransactionWithCommit()
950
    {
951
        $repository = $this->getRepository();
952
953
        /* BEGIN: Use Case */
954
        $sectionService = $repository->getSectionService();
955
956
        // Start a new transaction
957
        $repository->beginTransaction();
958
959
        try {
960
            // Get a create struct and set some properties
961
            $sectionCreate = $sectionService->newSectionCreateStruct();
962
            $sectionCreate->name = 'Test Section';
963
            $sectionCreate->identifier = self::SECTION_UNIQUE_KEY;
964
965
            // Create a new section
966
            $sectionService->createSection($sectionCreate);
967
968
            // Commit all changes
969
            $repository->commit();
970
        } catch (Exception $e) {
971
            // Cleanup hanging transaction on error
972
            $repository->rollback();
973
            throw $e;
974
        }
975
976
        // Load new section
977
        $section = $sectionService->loadSectionByIdentifier(self::SECTION_UNIQUE_KEY);
978
        /* END: Use Case */
979
980
        $this->assertEquals(self::SECTION_UNIQUE_KEY, $section->identifier);
981
    }
982
983
    /**
984
     * Test for the createSection() method.
985
     *
986
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
987
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
988
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
989
     */
990
    public function testUpdateSectionInTransactionWithRollback()
991
    {
992
        $repository = $this->getRepository();
993
994
        /* BEGIN: Use Case */
995
        $sectionService = $repository->getSectionService();
996
997
        // Start a new transaction
998
        $repository->beginTransaction();
999
1000
        try {
1001
            // Load standard section
1002
            $section = $sectionService->loadSectionByIdentifier('standard');
1003
1004
            // Get an update struct and change section name
1005
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
1006
            $sectionUpdate->name = 'My Standard';
1007
1008
            // Update section
1009
            $sectionService->updateSection($section, $sectionUpdate);
1010
        } catch (Exception $e) {
1011
            // Cleanup hanging transaction on error
1012
            $repository->rollback();
1013
            throw $e;
1014
        }
1015
1016
        // Rollback all changes
1017
        $repository->rollback();
1018
1019
        // Load updated section, name will still be "Standard"
1020
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
1021
        /* END: Use Case */
1022
1023
        $this->assertEquals('Standard', $updatedStandard->name);
1024
    }
1025
1026
    /**
1027
     * Test for the createSection() method.
1028
     *
1029
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
1030
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
1031
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
1032
     */
1033
    public function testUpdateSectionInTransactionWithCommit()
1034
    {
1035
        $repository = $this->getRepository();
1036
1037
        /* BEGIN: Use Case */
1038
        $sectionService = $repository->getSectionService();
1039
1040
        // Start a new transaction
1041
        $repository->beginTransaction();
1042
1043
        try {
1044
            // Load standard section
1045
            $section = $sectionService->loadSectionByIdentifier('standard');
1046
1047
            // Get an update struct and change section name
1048
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
1049
            $sectionUpdate->name = 'My Standard';
1050
1051
            // Update section
1052
            $sectionService->updateSection($section, $sectionUpdate);
1053
1054
            // Commit all changes
1055
            $repository->commit();
1056
        } catch (Exception $e) {
1057
            // Cleanup hanging transaction on error
1058
            $repository->rollback();
1059
            throw $e;
1060
        }
1061
1062
        // Load updated section, name will now be "My Standard"
1063
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
1064
        /* END: Use Case */
1065
1066
        $this->assertEquals('My Standard', $updatedStandard->name);
1067
    }
1068
}
1069