Completed
Push — master ( cfe8d7...fcc746 )
by André
19:36 queued 06:48
created

URLServiceAuthorizationTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 113
Duplicated Lines 54.87 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 62
loc 113
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testFindUrlsThrowsUnauthorizedException() 0 21 1
A testUpdateUrlThrowsUnauthorizedException() 23 23 1
A testLoadByIdThrowsUnauthorizedException() 19 19 1
A testLoadByUrlThrowsUnauthorizedException() 20 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\API\Repository\Tests;
8
9
use eZ\Publish\API\Repository\Values\URL\Query\Criterion as Criterion;
10
use eZ\Publish\API\Repository\Values\URL\URLQuery;
11
12
class URLServiceAuthorizationTest extends BaseURLServiceTest
13
{
14
    /**
15
     * Test for the findUrls() method.
16
     *
17
     * @see \eZ\Publish\API\Repository\URLService::findUrls
18
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
19
     */
20
    public function testFindUrlsThrowsUnauthorizedException()
21
    {
22
        $repository = $this->getRepository();
23
24
        $anonymousUserId = $this->generateId('user', 10);
25
        /* BEGIN: Use Case */
26
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
27
        // Publish demo installation.
28
29
        $userService = $repository->getUserService();
30
        $urlService = $repository->getURLService();
31
32
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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...
33
34
        $query = new URLQuery();
35
        $query->filter = new Criterion\MatchAll();
36
37
        // This call will fail with an UnauthorizedException
38
        $urlService->findUrls($query);
39
        /* END: Use Case */
40
    }
41
42
    /**
43
     * Test for the updateUrl() method.
44
     *
45
     * @see \eZ\Publish\API\Repository\URLService::updateUrl
46
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
47
     */
48 View Code Duplication
    public function testUpdateUrlThrowsUnauthorizedException()
49
    {
50
        $repository = $this->getRepository();
51
52
        $anonymousUserId = $this->generateId('user', 10);
53
        $urlId = $this->generateId('url', 23);
54
        /* BEGIN: Use Case */
55
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
56
        // Publish demo installation.
57
58
        $userService = $repository->getUserService();
59
        $urlService = $repository->getURLService();
60
61
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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...
62
63
        $url = $urlService->loadById($urlId);
64
        $updateStruct = $urlService->createUpdateStruct();
65
        $updateStruct->url = 'https://vimeo.com/';
66
67
        // This call will fail with an UnauthorizedException
68
        $urlService->updateUrl($url, $updateStruct);
69
        /* END: Use Case */
70
    }
71
72
    /**
73
     * Test for the loadById() method.
74
     *
75
     * @see \eZ\Publish\API\Repository\URLService::loadById
76
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
77
     */
78 View Code Duplication
    public function testLoadByIdThrowsUnauthorizedException()
79
    {
80
        $repository = $this->getRepository();
81
82
        $anonymousUserId = $this->generateId('user', 10);
83
        $urlId = $this->generateId('url', 23);
84
        /* BEGIN: Use Case */
85
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
86
        // Publish demo installation.
87
88
        $userService = $repository->getUserService();
89
        $urlService = $repository->getURLService();
90
91
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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...
92
93
        // This call will fail with an UnauthorizedException
94
        $urlService->loadById($urlId);
95
        /* END: Use Case */
96
    }
97
98
    /**
99
     * Test for the loadByUrl() method.
100
     *
101
     * @see \eZ\Publish\API\Repository\URLService::loadById
102
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
103
     */
104 View Code Duplication
    public function testLoadByUrlThrowsUnauthorizedException()
105
    {
106
        $repository = $this->getRepository();
107
108
        $anonymousUserId = $this->generateId('user', 10);
109
        $url = '/content/view/sitemap/2';
110
111
        /* BEGIN: Use Case */
112
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
113
        // Publish demo installation.
114
115
        $userService = $repository->getUserService();
116
        $urlService = $repository->getURLService();
117
118
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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...
119
120
        // This call will fail with an UnauthorizedException
121
        $urlService->loadByUrl($url);
122
        /* END: Use Case */
123
    }
124
}
125