Completed
Push — ezp30878_cant_add_image_with_p... ( e19ea7...263f1b )
by
unknown
20:16
created

SectionServiceTest::testAssignSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 50
rs 9.0909
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
15
/**
16
 * Test case for operations in the SectionService using in memory storage.
17
 *
18
 * @see eZ\Publish\API\Repository\SectionService
19
 * @group integration
20
 * @group section
21
 */
22
class SectionServiceTest extends BaseTest
23
{
24
    /**
25
     * Tests that the required <b>ContentService::loadContentInfoByRemoteId()</b>
26
     * at least returns an object, because this method is utilized in several
27
     * tests,.
28
     */
29 View Code Duplication
    protected function setUp()
30
    {
31
        parent::setUp();
32
33
        try {
34
            // RemoteId of the "Media" page of an eZ Publish demo installation
35
            $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
36
37
            // Load the ContentService
38
            $contentService = $this->getRepository()->getContentService();
39
40
            // Load a content info instance
41
            $contentInfo = $contentService->loadContentInfoByRemoteId(
42
                $mediaRemoteId
43
            );
44
45
            if (false === is_object($contentInfo)) {
46
                $this->markTestSkipped(
47
                    'This test cannot be executed, because the utilized ' .
48
                    'ContentService::loadContentInfoByRemoteId() does not ' .
49
                    'return an object.'
50
                );
51
            }
52
        } catch (Exception $e) {
53
            $this->markTestSkipped(
54
                'This test cannot be executed, because the utilized ' .
55
                'ContentService::loadContentInfoByRemoteId() failed with ' .
56
                PHP_EOL . PHP_EOL .
57
                $e
58
            );
59
        }
60
    }
61
62
    /**
63
     * Test for the newSectionCreateStruct() method.
64
     *
65
     * @see \eZ\Publish\API\Repository\SectionService::newSectionCreateStruct()
66
     */
67
    public function testNewSectionCreateStruct()
68
    {
69
        $repository = $this->getRepository();
70
71
        /* BEGIN: Use Case */
72
        $sectionService = $repository->getSectionService();
73
74
        $sectionCreate = $sectionService->newSectionCreateStruct();
75
        /* END: Use Case */
76
77
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\SectionCreateStruct', $sectionCreate);
78
    }
79
80
    /**
81
     * Test for the createSection() method.
82
     *
83
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
84
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionCreateStruct
85
     */
86
    public function testCreateSection()
87
    {
88
        $repository = $this->getRepository();
89
90
        /* BEGIN: Use Case */
91
        $sectionService = $repository->getSectionService();
92
93
        $sectionCreate = $sectionService->newSectionCreateStruct();
94
        $sectionCreate->name = 'Test Section';
95
        $sectionCreate->identifier = 'uniqueKey';
96
97
        $section = $sectionService->createSection($sectionCreate);
98
        /* END: Use Case */
99
100
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Section', $section);
101
    }
102
103
    /**
104
     * Test for the createSection() method.
105
     *
106
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
107
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
108
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
109
     */
110
    public function testCreateSectionThrowsInvalidArgumentException()
111
    {
112
        $repository = $this->getRepository();
113
114
        /* BEGIN: Use Case */
115
        $sectionService = $repository->getSectionService();
116
117
        $sectionCreateOne = $sectionService->newSectionCreateStruct();
118
        $sectionCreateOne->name = 'Test section one';
119
        $sectionCreateOne->identifier = 'uniqueKey';
120
121
        $sectionService->createSection($sectionCreateOne);
122
123
        $sectionCreateTwo = $sectionService->newSectionCreateStruct();
124
        $sectionCreateTwo->name = 'Test section two';
125
        $sectionCreateTwo->identifier = 'uniqueKey';
126
127
        // This will fail, because identifier uniqueKey already exists.
128
        $sectionService->createSection($sectionCreateTwo);
129
        /* END: Use Case */
130
    }
131
132
    /**
133
     * Test for the loadSection() method.
134
     *
135
     * @see \eZ\Publish\API\Repository\SectionService::loadSection()
136
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
137
     */
138
    public function testLoadSection()
139
    {
140
        $repository = $this->getRepository();
141
142
        $sectionId = $this->generateId('section', 2);
143
        /* BEGIN: Use Case */
144
        $sectionService = $repository->getSectionService();
145
146
        // Loads user section
147
        // $sectionId contains the corresponding ID
148
        $section = $sectionService->loadSection($sectionId);
149
        /* END: Use Case */
150
151
        $this->assertEquals('users', $section->identifier);
152
    }
153
154
    /**
155
     * Test for the loadSection() method.
156
     *
157
     * @see \eZ\Publish\API\Repository\SectionService::loadSection()
158
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
159
     */
160
    public function testLoadSectionThrowsNotFoundException()
161
    {
162
        $repository = $this->getRepository();
163
164
        $nonExistentSectionId = $this->generateId('section', self::DB_INT_MAX);
165
        /* BEGIN: Use Case */
166
        $sectionService = $repository->getSectionService();
167
168
        // This call should fail with a NotFoundException
169
        // $nonExistentSectionId contains a section ID that is not known
170
        $sectionService->loadSection($nonExistentSectionId);
171
        /* END: Use Case */
172
    }
173
174
    /**
175
     * Test for the newSectionUpdateStruct() method.
176
     *
177
     * @see \eZ\Publish\API\Repository\SectionService::newSectionUpdateStruct()
178
     */
179
    public function testNewSectionUpdateStruct()
180
    {
181
        $repository = $this->getRepository();
182
183
        /* BEGIN: Use Case */
184
        $sectionService = $repository->getSectionService();
185
186
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
187
        /* END: Use Case */
188
189
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\SectionUpdateStruct', $sectionUpdate);
190
    }
191
192
    /**
193
     * Test for the updateSection() method.
194
     *
195
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
196
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
197
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
198
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionUpdateStruct
199
     */
200
    public function testUpdateSection()
201
    {
202
        $repository = $this->getRepository();
203
204
        $standardSectionId = $this->generateId('section', 1);
205
        /* BEGIN: Use Case */
206
        // $standardSectionId contains the ID of the "Standard" section in a eZ
207
        // Publish demo installation.
208
209
        $sectionService = $repository->getSectionService();
210
211
        $section = $sectionService->loadSection($standardSectionId);
212
213
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
214
        $sectionUpdate->name = 'New section name';
215
        $sectionUpdate->identifier = 'newUniqueKey';
216
217
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
218
        /* END: Use Case */
219
220
        // Verify that service returns an instance of Section
221
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Section', $updatedSection);
222
223
        // Verify that the service also persists the changes
224
        $updatedSection = $sectionService->loadSection($standardSectionId);
225
226
        $this->assertEquals('New section name', $updatedSection->name);
227
    }
228
229
    /**
230
     * Test for the updateSection() method.
231
     *
232
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
233
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
234
     */
235
    public function testUpdateSectionKeepsSectionIdentifierOnNameUpdate()
236
    {
237
        $repository = $this->getRepository();
238
239
        $standardSectionId = $this->generateId('section', 1);
240
        /* BEGIN: Use Case */
241
        // $standardSectionId contains the ID of the "Standard" section in a eZ
242
        // Publish demo installation.
243
244
        $sectionService = $repository->getSectionService();
245
246
        $section = $sectionService->loadSection($standardSectionId);
247
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
248
        $sectionUpdate->name = 'New section name';
249
250
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
251
        /* END: Use Case */
252
253
        $this->assertEquals('standard', $updatedSection->identifier);
254
    }
255
256
    /**
257
     * Test for the updateSection() method.
258
     *
259
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
260
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
261
     */
262
    public function testUpdateSectionWithSectionIdentifierOnNameUpdate()
263
    {
264
        $repository = $this->getRepository();
265
266
        $standardSectionId = $this->generateId('section', 1);
267
        /* BEGIN: Use Case */
268
        // $standardSectionId contains the ID of the "Standard" section in a eZ
269
        // Publish demo installation.
270
271
        $sectionService = $repository->getSectionService();
272
273
        $section = $sectionService->loadSection($standardSectionId);
274
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
275
        $sectionUpdate->name = 'New section name';
276
277
        // section identifier remains the same
278
        $sectionUpdate->identifier = $section->identifier;
279
280
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
281
        /* END: Use Case */
282
283
        $this->assertEquals('standard', $updatedSection->identifier);
284
    }
285
286
    /**
287
     * Test for the updateSection() method.
288
     *
289
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
290
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
291
     */
292
    public function testUpdateSectionKeepsSectionNameOnIdentifierUpdate()
293
    {
294
        $repository = $this->getRepository();
295
296
        $standardSectionId = $this->generateId('section', 1);
297
        /* BEGIN: Use Case */
298
        // $standardSectionId contains the ID of the "Standard" section in a eZ
299
        // Publish demo installation.
300
301
        $sectionService = $repository->getSectionService();
302
303
        $section = $sectionService->loadSection($standardSectionId);
304
305
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
306
        $sectionUpdate->identifier = 'newUniqueKey';
307
308
        $updatedSection = $sectionService->updateSection($section, $sectionUpdate);
309
        /* END: Use Case */
310
311
        $this->assertEquals('Standard', $updatedSection->name);
312
    }
313
314
    /**
315
     * Test for the updateSection() method.
316
     *
317
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
318
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
319
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
320
     */
321
    public function testUpdateSectionThrowsInvalidArgumentException()
322
    {
323
        $repository = $this->getRepository();
324
325
        $standardSectionId = $this->generateId('section', 1);
326
        /* BEGIN: Use Case */
327
        // $standardSectionId contains the ID of the "Standard" section in a eZ
328
        // Publish demo installation.
329
330
        $sectionService = $repository->getSectionService();
331
332
        // Create section with conflict identifier
333
        $sectionCreate = $sectionService->newSectionCreateStruct();
334
        $sectionCreate->name = 'Conflict section';
335
        $sectionCreate->identifier = 'conflictKey';
336
337
        $sectionService->createSection($sectionCreate);
338
339
        // Load an existing section and update to an existing identifier
340
        $section = $sectionService->loadSection($standardSectionId);
341
342
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
343
        $sectionUpdate->identifier = 'conflictKey';
344
345
        // This call should fail with an InvalidArgumentException
346
        $sectionService->updateSection($section, $sectionUpdate);
347
        /* END: Use Case */
348
    }
349
350
    /**
351
     * Test for the loadSections() method.
352
     *
353
     * @see \eZ\Publish\API\Repository\SectionService::loadSections()
354
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
355
     */
356
    public function testLoadSections()
357
    {
358
        $repository = $this->getRepository();
359
360
        /* BEGIN: Use Case */
361
        $sectionService = $repository->getSectionService();
362
363
        $sections = $sectionService->loadSections();
364
        foreach ($sections as $section) {
365
            // Operate on all sections.
366
        }
367
        /* END: Use Case */
368
369
        $this->assertEquals(6, count($sections));
370
    }
371
372
    /**
373
     * Test for the loadSections() method.
374
     *
375
     * @see \eZ\Publish\API\Repository\SectionService::loadSections()
376
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
377
     */
378
    public function testLoadSectionsReturnsDefaultSectionsByDefault()
379
    {
380
        $repository = $this->getRepository();
381
382
        $sectionService = $repository->getSectionService();
383
384
        $this->assertEquals(
385
            [
386
                new Section(
387
                    [
388
                        'id' => $this->generateId('section', 1),
389
                        'name' => 'Standard',
390
                        'identifier' => 'standard',
391
                    ]
392
                ),
393
                new Section(
394
                    [
395
                        'id' => $this->generateId('section', 2),
396
                        'name' => 'Users',
397
                        'identifier' => 'users',
398
                    ]
399
                ),
400
                new Section(
401
                    [
402
                        'id' => $this->generateId('section', 3),
403
                        'name' => 'Media',
404
                        'identifier' => 'media',
405
                    ]
406
                ),
407
                new Section(
408
                    [
409
                        'id' => $this->generateId('section', 4),
410
                        'name' => 'Setup',
411
                        'identifier' => 'setup',
412
                    ]
413
                ),
414
                new Section(
415
                    [
416
                        'id' => $this->generateId('section', 5),
417
                        'name' => 'Design',
418
                        'identifier' => 'design',
419
                    ]
420
                ),
421
                new Section(
422
                    [
423
                        'id' => $this->generateId('section', 6),
424
                        'name' => 'Restricted',
425
                        'identifier' => '',
426
                    ]
427
                ),
428
            ],
429
            $sectionService->loadSections()
430
        );
431
    }
432
433
    /**
434
     * Test for the loadSectionByIdentifier() method.
435
     *
436
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
437
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
438
     */
439 View Code Duplication
    public function testLoadSectionByIdentifier()
440
    {
441
        $repository = $this->getRepository();
442
443
        /* BEGIN: Use Case */
444
        $sectionService = $repository->getSectionService();
445
446
        $sectionCreate = $sectionService->newSectionCreateStruct();
447
        $sectionCreate->name = 'Test Section';
448
        $sectionCreate->identifier = 'uniqueKey';
449
450
        $sectionId = $sectionService->createSection($sectionCreate)->id;
451
452
        $section = $sectionService->loadSectionByIdentifier('uniqueKey');
453
        /* END: Use Case */
454
455
        $this->assertEquals($sectionId, $section->id);
456
    }
457
458
    /**
459
     * Test for the loadSectionByIdentifier() method.
460
     *
461
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
462
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
463
     */
464
    public function testLoadSectionByIdentifierThrowsNotFoundException()
465
    {
466
        $repository = $this->getRepository();
467
468
        /* BEGIN: Use Case */
469
        $sectionService = $repository->getSectionService();
470
471
        // This call should fail with a NotFoundException
472
        $sectionService->loadSectionByIdentifier('someUnknownSectionIdentifier');
473
        /* END: Use Case */
474
    }
475
476
    /**
477
     * Test for the countAssignedContents() method.
478
     *
479
     * @see \eZ\Publish\API\Repository\SectionService::countAssignedContents()
480
     */
481 View Code Duplication
    public function testCountAssignedContents()
482
    {
483
        $repository = $this->getRepository();
484
485
        $sectionService = $repository->getSectionService();
486
487
        $standardSectionId = $this->generateId('section', 1);
488
        /* BEGIN: Use Case */
489
        // $standardSectionId contains the ID of the "Standard" section in a eZ
490
        // Publish demo installation.
491
492
        $standardSection = $sectionService->loadSection($standardSectionId);
493
494
        $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...
495
            $standardSection
496
        );
497
        /* END: Use Case */
498
499
        $this->assertEquals(
500
            2, // Taken from the fixture
501
            $numberOfAssignedContent
502
        );
503
    }
504
505
    /**
506
     * Test for the isSectionUsed() method.
507
     *
508
     * @see \eZ\Publish\API\Repository\SectionService::isSectionUsed()
509
     */
510 View Code Duplication
    public function testIsSectionUsed()
511
    {
512
        $repository = $this->getRepository();
513
514
        $sectionService = $repository->getSectionService();
515
516
        $standardSectionId = $this->generateId('section', 1);
517
        /* BEGIN: Use Case */
518
        // $standardSectionId contains the ID of the "Standard" section in a eZ
519
        // Publish demo installation.
520
521
        $standardSection = $sectionService->loadSection($standardSectionId);
522
523
        $isSectionUsed = $sectionService->isSectionUsed(
524
            $standardSection
525
        );
526
        /* END: Use Case */
527
528
        $this->assertTrue(
529
            // Taken from the fixture
530
            $isSectionUsed
531
        );
532
    }
533
534
    /**
535
     * Test for the assignSection() method.
536
     *
537
     * @see \eZ\Publish\API\Repository\SectionService::assignSection()
538
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCountAssignedContents
539
     */
540
    public function testAssignSection()
541
    {
542
        $repository = $this->getRepository();
543
        $sectionService = $repository->getSectionService();
544
545
        $standardSectionId = $this->generateId('section', 1);
546
        $mediaSectionId = $this->generateId('section', 3);
547
548
        $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...
549
            $sectionService->loadSection($standardSectionId)
550
        );
551
        $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...
552
            $sectionService->loadSection($mediaSectionId)
553
        );
554
555
        /* BEGIN: Use Case */
556
        // $mediaSectionId contains the ID of the "Media" section in a eZ
557
        // Publish demo installation.
558
559
        // RemoteId of the "Media" page of an eZ Publish demo installation
560
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
561
562
        $contentService = $repository->getContentService();
563
        $sectionService = $repository->getSectionService();
564
565
        // Load a content info instance
566
        $contentInfo = $contentService->loadContentInfoByRemoteId(
567
            $mediaRemoteId
568
        );
569
570
        // Load the "Standard" section
571
        $section = $sectionService->loadSection($standardSectionId);
572
573
        // Assign Section to ContentInfo
574
        $sectionService->assignSection($contentInfo, $section);
575
        /* END: Use Case */
576
577
        $this->assertEquals(
578
            $beforeStandardCount + 1,
579
            $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...
580
                $sectionService->loadSection($standardSectionId)
581
            )
582
        );
583
        $this->assertEquals(
584
            $beforeMediaCount - 1,
585
            $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...
586
                $sectionService->loadSection($mediaSectionId)
587
            )
588
        );
589
    }
590
591
    /**
592
     * Test for the countAssignedContents() method.
593
     *
594
     * @see \eZ\Publish\API\Repository\SectionService::countAssignedContents()
595
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
596
     */
597
    public function testCountAssignedContentsReturnsZeroByDefault()
598
    {
599
        $repository = $this->getRepository();
600
601
        /* BEGIN: Use Case */
602
        $sectionService = $repository->getSectionService();
603
604
        $sectionCreate = $sectionService->newSectionCreateStruct();
605
        $sectionCreate->name = 'Test Section';
606
        $sectionCreate->identifier = 'uniqueKey';
607
608
        $section = $sectionService->createSection($sectionCreate);
609
610
        // The number of assigned contents should be zero
611
        $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...
612
        /* END: Use Case */
613
614
        $this->assertSame(0, $assignedContents);
615
    }
616
617
    /**
618
     * Test for the isSectionUsed() method.
619
     *
620
     * @see \eZ\Publish\API\Repository\SectionService::isSectionUsed()
621
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
622
     */
623
    public function testIsSectionUsedReturnsZeroByDefault()
624
    {
625
        $repository = $this->getRepository();
626
627
        /* BEGIN: Use Case */
628
        $sectionService = $repository->getSectionService();
629
630
        $sectionCreate = $sectionService->newSectionCreateStruct();
631
        $sectionCreate->name = 'Test Section';
632
        $sectionCreate->identifier = 'uniqueKey';
633
634
        $section = $sectionService->createSection($sectionCreate);
635
636
        // The number of assigned contents should be zero
637
        $isSectionUsed = $sectionService->isSectionUsed($section);
638
        /* END: Use Case */
639
640
        $this->assertFalse($isSectionUsed);
641
    }
642
643
    /**
644
     * Test for the deleteSection() method.
645
     *
646
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
647
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSections
648
     */
649
    public function testDeleteSection()
650
    {
651
        $repository = $this->getRepository();
652
653
        /* BEGIN: Use Case */
654
        $sectionService = $repository->getSectionService();
655
656
        $sectionCreate = $sectionService->newSectionCreateStruct();
657
        $sectionCreate->name = 'Test Section';
658
        $sectionCreate->identifier = 'uniqueKey';
659
660
        $section = $sectionService->createSection($sectionCreate);
661
662
        // Delete the newly created section
663
        $sectionService->deleteSection($section);
664
        /* END: Use Case */
665
666
        $this->assertEquals(6, count($sectionService->loadSections()));
667
    }
668
669
    /**
670
     * Test for the deleteSection() method.
671
     *
672
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
673
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
674
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testDeleteSection
675
     */
676
    public function testDeleteSectionThrowsNotFoundException()
677
    {
678
        $repository = $this->getRepository();
679
680
        /* BEGIN: Use Case */
681
        $sectionService = $repository->getSectionService();
682
683
        $sectionCreate = $sectionService->newSectionCreateStruct();
684
        $sectionCreate->name = 'Test Section';
685
        $sectionCreate->identifier = 'uniqueKey';
686
687
        $section = $sectionService->createSection($sectionCreate);
688
689
        // Delete the newly created section
690
        $sectionService->deleteSection($section);
691
692
        // This call should fail with a NotFoundException
693
        $sectionService->deleteSection($section);
694
        /* END: Use Case */
695
    }
696
697
    /**
698
     * Test for the deleteSection() method.
699
     *
700
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
701
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
702
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testAssignSection
703
     */
704
    public function testDeleteSectionThrowsBadStateException()
705
    {
706
        $repository = $this->getRepository();
707
708
        $standardSectionId = $this->generateId('section', 1);
709
        /* BEGIN: Use Case */
710
        // $standardSectionId contains the ID of the "Standard" section in a eZ
711
        // Publish demo installation.
712
713
        // RemoteId of the "Media" page of an eZ Publish demo installation
714
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
715
716
        $contentService = $repository->getContentService();
717
        $sectionService = $repository->getSectionService();
718
719
        // Load the "Media" ContentInfo
720
        $contentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
721
722
        // Load the "Standard" section
723
        $section = $sectionService->loadSection($standardSectionId);
724
725
        // Assign "Media" to "Standard" section
726
        $sectionService->assignSection($contentInfo, $section);
727
728
        // This call should fail with a BadStateException, because there are assigned contents
729
        $sectionService->deleteSection($section);
730
        /* END: Use Case */
731
    }
732
733
    /**
734
     * Test for the createSection() method.
735
     *
736
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
737
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
738
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
739
     */
740 View Code Duplication
    public function testCreateSectionInTransactionWithRollback()
741
    {
742
        $repository = $this->getRepository();
743
744
        /* BEGIN: Use Case */
745
        $sectionService = $repository->getSectionService();
746
747
        // Start a new transaction
748
        $repository->beginTransaction();
749
750
        try {
751
            // Get a create struct and set some properties
752
            $sectionCreate = $sectionService->newSectionCreateStruct();
753
            $sectionCreate->name = 'Test Section';
754
            $sectionCreate->identifier = 'uniqueKey';
755
756
            // Create a new section
757
            $sectionService->createSection($sectionCreate);
758
        } catch (Exception $e) {
759
            // Cleanup hanging transaction on error
760
            $repository->rollback();
761
            throw $e;
762
        }
763
764
        // Rollback all changes
765
        $repository->rollback();
766
767
        try {
768
            // This call will fail with a not found exception
769
            $sectionService->loadSectionByIdentifier('uniqueKey');
770
        } catch (NotFoundException $e) {
771
            // Expected execution path
772
        }
773
        /* END: Use Case */
774
775
        $this->assertTrue(isset($e), 'Can still load section after rollback.');
776
    }
777
778
    /**
779
     * Test for the createSection() method.
780
     *
781
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
782
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
783
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
784
     */
785
    public function testCreateSectionInTransactionWithCommit()
786
    {
787
        $repository = $this->getRepository();
788
789
        /* BEGIN: Use Case */
790
        $sectionService = $repository->getSectionService();
791
792
        // Start a new transaction
793
        $repository->beginTransaction();
794
795
        try {
796
            // Get a create struct and set some properties
797
            $sectionCreate = $sectionService->newSectionCreateStruct();
798
            $sectionCreate->name = 'Test Section';
799
            $sectionCreate->identifier = 'uniqueKey';
800
801
            // Create a new section
802
            $sectionService->createSection($sectionCreate);
803
804
            // Commit all changes
805
            $repository->commit();
806
        } catch (Exception $e) {
807
            // Cleanup hanging transaction on error
808
            $repository->rollback();
809
            throw $e;
810
        }
811
812
        // Load new section
813
        $section = $sectionService->loadSectionByIdentifier('uniqueKey');
814
        /* END: Use Case */
815
816
        $this->assertEquals('uniqueKey', $section->identifier);
817
    }
818
819
    /**
820
     * Test for the createSection() method.
821
     *
822
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
823
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
824
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
825
     */
826 View Code Duplication
    public function testUpdateSectionInTransactionWithRollback()
827
    {
828
        $repository = $this->getRepository();
829
830
        /* BEGIN: Use Case */
831
        $sectionService = $repository->getSectionService();
832
833
        // Start a new transaction
834
        $repository->beginTransaction();
835
836
        try {
837
            // Load standard section
838
            $section = $sectionService->loadSectionByIdentifier('standard');
839
840
            // Get an update struct and change section name
841
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
842
            $sectionUpdate->name = 'My Standard';
843
844
            // Update section
845
            $sectionService->updateSection($section, $sectionUpdate);
846
        } catch (Exception $e) {
847
            // Cleanup hanging transaction on error
848
            $repository->rollback();
849
            throw $e;
850
        }
851
852
        // Rollback all changes
853
        $repository->rollback();
854
855
        // Load updated section, name will still be "Standard"
856
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
857
        /* END: Use Case */
858
859
        $this->assertEquals('Standard', $updatedStandard->name);
860
    }
861
862
    /**
863
     * Test for the createSection() method.
864
     *
865
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
866
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
867
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
868
     */
869 View Code Duplication
    public function testUpdateSectionInTransactionWithCommit()
870
    {
871
        $repository = $this->getRepository();
872
873
        /* BEGIN: Use Case */
874
        $sectionService = $repository->getSectionService();
875
876
        // Start a new transaction
877
        $repository->beginTransaction();
878
879
        try {
880
            // Load standard section
881
            $section = $sectionService->loadSectionByIdentifier('standard');
882
883
            // Get an update struct and change section name
884
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
885
            $sectionUpdate->name = 'My Standard';
886
887
            // Update section
888
            $sectionService->updateSection($section, $sectionUpdate);
889
890
            // Commit all changes
891
            $repository->commit();
892
        } catch (Exception $e) {
893
            // Cleanup hanging transaction on error
894
            $repository->rollback();
895
            throw $e;
896
        }
897
898
        // Load updated section, name will now be "My Standard"
899
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
900
        /* END: Use Case */
901
902
        $this->assertEquals('My Standard', $updatedStandard->name);
903
    }
904
}
905