Completed
Push — master ( c423d6...fbe27e )
by André
25:21
created

testCreateUrlAliasThrowsUnauthorizedException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 23
loc 23
rs 9.0856
1
<?php
2
3
/**
4
 * File containing the URLAliasServiceTest 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
class URLAliasServiceAuthorizationTest extends BaseTest
12
{
13
    /**
14
     * Test for the createUrlAlias() method.
15
     *
16
     * @covers \eZ\Publish\API\Repository\URLAliasService::createUrlAlias()
17
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
18
     * @depends \eZ\Publish\API\Repository\Tests\URLAliasServiceTest::testCreateUrlAlias
19
     */
20 View Code Duplication
    public function testCreateUrlAliasThrowsUnauthorizedException()
21
    {
22
        $repository = $this->getRepository();
23
24
        $anonymousUserId = $this->generateId('user', 10);
25
        $parentLocationId = $this->generateId('location', 2);
26
        /* BEGIN: Use Case */
27
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
28
        // Publish demo installation.
29
        // $locationId is the ID of an existing location
30
        $userService = $repository->getUserService();
31
        $urlAliasService = $repository->getURLAliasService();
32
        $locationService = $repository->getLocationService();
33
34
        $location = $locationService->newLocationCreateStruct($parentLocationId);
35
36
        $anonymousUser = $userService->loadUser($anonymousUserId);
37
        $repository->getPermissionResolver()->setCurrentUserReference($anonymousUser);
38
39
        // This call will fail with an UnauthorizedException
40
        $urlAliasService->createUrlAlias($location, '/Home/My-New-Site', 'eng-US');
41
        /* END: Use Case */
42
    }
43
44
    /**
45
     * Test for the createGlobalUrlAlias() method.
46
     *
47
     * @covers \eZ\Publish\API\Repository\URLAliasService::createGlobalUrlAlias()
48
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
49
     * @depends \eZ\Publish\API\Repository\Tests\URLAliasServiceTest::testCreateGlobalUrlAlias
50
     */
51
    public function testCreateGlobalUrlAliasThrowsUnauthorizedException()
52
    {
53
        $repository = $this->getRepository();
54
55
        $anonymousUserId = $this->generateId('user', 10);
56
        /* BEGIN: Use Case */
57
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
58
        // Publish demo installation.
59
        $userService = $repository->getUserService();
60
        $urlAliasService = $repository->getURLAliasService();
61
62
        $anonymousUser = $userService->loadUser($anonymousUserId);
63
        $repository->getPermissionResolver()->setCurrentUserReference($anonymousUser);
64
65
        // This call will fail with an UnauthorizedException
66
        $urlAliasService->createGlobalUrlAlias('module:content/search?SearchText=eZ', '/Home/My-New-Site', 'eng-US');
67
        /* END: Use Case */
68
    }
69
70
    /**
71
     * Test for the removeAliases() method.
72
     *
73
     * @covers \eZ\Publish\API\Repository\URLAliasService::removeAliases()
74
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
75
     * @depends \eZ\Publish\API\Repository\Tests\URLAliasServiceTest::testRemoveAliases
76
     */
77 View Code Duplication
    public function testRemoveAliasesThrowsUnauthorizedException()
78
    {
79
        $repository = $this->getRepository();
80
        $anonymousUserId = $this->generateId('user', 10);
81
82
        $locationService = $repository->getLocationService();
83
        $someLocation = $locationService->loadLocation(
84
            $this->generateId('location', 12)
85
        );
86
87
        /* BEGIN: Use Case */
88
        // $someLocation contains a location with automatically generated
89
        // aliases assigned
90
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
91
        $urlAliasService = $repository->getURLAliasService();
92
        $userService = $repository->getUserService();
93
94
        $anonymousUser = $userService->loadUser($anonymousUserId);
95
        $repository->getPermissionResolver()->setCurrentUserReference($anonymousUser);
96
97
        $initialAliases = $urlAliasService->listLocationAliases($someLocation);
98
99
        // This call will fail with an UnauthorizedException
100
        $urlAliasService->removeAliases($initialAliases);
101
        /* END: Use Case */
102
    }
103
}
104