Code Duplication    Length = 23-28 lines in 6 locations

eZ/Publish/API/Repository/Tests/SectionServiceAuthorizationTest.php 3 locations

@@ 60-83 (lines=24) @@
57
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
58
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
59
     */
60
    public function testLoadSectionThrowsUnauthorizedException()
61
    {
62
        $repository = $this->getRepository();
63
64
        $anonymousUserId = $this->generateId('user', 10);
65
        /* BEGIN: Use Case */
66
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
67
        // Publish demo installation.
68
        $userService = $repository->getUserService();
69
        $sectionService = $repository->getSectionService();
70
71
        $sectionCreate = $sectionService->newSectionCreateStruct();
72
        $sectionCreate->name = 'Test Section';
73
        $sectionCreate->identifier = 'uniqueKey';
74
75
        $sectionId = $sectionService->createSection($sectionCreate)->id;
76
77
        // Set anonymous user
78
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
79
80
        // This call will fail with a "UnauthorizedException"
81
        $sectionService->loadSection($sectionId);
82
        /* END: Use Case */
83
    }
84
85
    /**
86
     * Test for the updateSection() method.
@@ 92-119 (lines=28) @@
89
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
90
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
91
     */
92
    public function testUpdateSectionThrowsUnauthorizedException()
93
    {
94
        $repository = $this->getRepository();
95
96
        $standardSectionId = $this->generateId('section', 1);
97
        $anonymousUserId = $this->generateId('user', 10);
98
        /* BEGIN: Use Case */
99
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
100
        // Publish demo installation.
101
        // $standardSectionId is the ID of the "Standard" section in a eZ
102
        // Publish demo installation.
103
104
        $userService = $repository->getUserService();
105
        $sectionService = $repository->getSectionService();
106
107
        $section = $sectionService->loadSection($standardSectionId);
108
109
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
110
        $sectionUpdate->name = 'New section name';
111
        $sectionUpdate->identifier = 'newUniqueKey';
112
113
        // Set anonymous user
114
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
115
116
        // This call will fail with a "UnauthorizedException"
117
        $sectionService->updateSection($section, $sectionUpdate);
118
        /* END: Use Case */
119
    }
120
121
    /**
122
     * Test for the loadSections() method.
@@ 237-260 (lines=24) @@
234
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
235
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
236
     */
237
    public function testDeleteSectionThrowsUnauthorizedException()
238
    {
239
        $repository = $this->getRepository();
240
241
        $anonymousUserId = $this->generateId('user', 10);
242
        /* BEGIN: Use Case */
243
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
244
        // Publish demo installation.
245
        $userService = $repository->getUserService();
246
        $sectionService = $repository->getSectionService();
247
248
        $sectionCreate = $sectionService->newSectionCreateStruct();
249
        $sectionCreate->name = 'Test Section';
250
        $sectionCreate->identifier = 'uniqueKey';
251
252
        $section = $sectionService->createSection($sectionCreate);
253
254
        // Set anonymous user
255
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
256
257
        // This call will fail with a "UnauthorizedException"
258
        $sectionService->deleteSection($section);
259
        /* END: Use Case */
260
    }
261
}
262

eZ/Publish/API/Repository/Tests/SectionServiceTest.php 3 locations

@@ 202-229 (lines=28) @@
199
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
200
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testNewSectionUpdateStruct
201
     */
202
    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.
@@ 264-286 (lines=23) @@
261
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
262
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
263
     */
264
    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.
@@ 323-350 (lines=28) @@
320
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
321
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
322
     */
323
    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.