Completed
Push — ezp-23733_disable_section_dele... ( 6e64a4...9f6957 )
by
unknown
26:00
created

testIsSectionUsedReturnsZeroByDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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