Completed
Push — test-EZP-26707-issearchable-fu... ( 965a07...6812c2 )
by
unknown
33:45 queued 07:39
created

TrashServiceTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 574
Duplicated Lines 12.72 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 73
loc 574
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 11

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testTrash() 0 11 1
B testTrashSetsExpectedTrashItemProperties() 0 27 1
A testTrashRemovesLocationFromMainStorage() 0 17 1
B testTrashRemovesChildLocationsFromMainStorage() 0 29 3
A testTrashDecrementsChildCountOnParentLocation() 20 20 1
A testLoadTrashItem() 0 22 1
A testLoadTrashItemThrowsNotFoundException() 0 13 1
B testRecover() 0 37 2
B testRecoverDoesNotRestoreChildLocations() 0 43 4
B testRecoverWithLocationCreateStructParameter() 0 43 2
B testRecoverIncrementsChildCountOnOriginalParent() 0 33 2
B testRecoverWithLocationCreateStructParameterIncrementsChildCountOnNewParent() 0 39 2
B testFindTrashItems() 28 28 1
B testEmptyTrash() 25 25 1
B testDeleteTrashItem() 0 45 1
A createRemoteIdList() 0 23 3

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
 * File containing the TrashServiceTest 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\Values\Content\Location;
14
use eZ\Publish\API\Repository\Values\Content\Query;
15
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
16
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
17
18
/**
19
 * Test case for operations in the TrashService using in memory storage.
20
 *
21
 * @see eZ\Publish\API\Repository\TrashService
22
 * @group integration
23
 * @group trash
24
 */
25
class TrashServiceTest extends BaseTrashServiceTest
26
{
27
    /**
28
     * Test for the trash() method.
29
     *
30
     * @see \eZ\Publish\API\Repository\TrashService::trash()
31
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
32
     */
33
    public function testTrash()
34
    {
35
        /* BEGIN: Use Case */
36
        $trashItem = $this->createTrashItem();
37
        /* END: Use Case */
38
39
        $this->assertInstanceOf(
40
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\TrashItem',
41
            $trashItem
42
        );
43
    }
44
45
    /**
46
     * Test for the trash() method.
47
     *
48
     * @see \eZ\Publish\API\Repository\TrashService::trash()
49
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
50
     */
51
    public function testTrashSetsExpectedTrashItemProperties()
52
    {
53
        $repository = $this->getRepository();
54
55
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
56
57
        // Load the location that will be trashed
58
        $location = $repository->getLocationService()
59
            ->loadLocationByRemoteId($mediaRemoteId);
60
61
        $expected = array(
62
            'id' => $location->id,
63
            'depth' => $location->depth,
64
            'hidden' => $location->hidden,
65
            'invisible' => $location->invisible,
66
            'parentLocationId' => $location->parentLocationId,
67
            'pathString' => $location->pathString,
68
            'priority' => $location->priority,
69
            'remoteId' => $location->remoteId,
70
            'sortField' => $location->sortField,
71
            'sortOrder' => $location->sortOrder,
72
        );
73
74
        $trashItem = $this->createTrashItem();
75
76
        $this->assertPropertiesCorrect($expected, $trashItem);
77
    }
78
79
    /**
80
     * Test for the trash() method.
81
     *
82
     * @see \eZ\Publish\API\Repository\TrashService::trash()
83
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
84
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
85
     */
86
    public function testTrashRemovesLocationFromMainStorage()
87
    {
88
        $repository = $this->getRepository();
89
90
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
91
92
        /* BEGIN: Use Case */
93
        $this->createTrashItem();
94
95
        // Load the location service
96
        $locationService = $repository->getLocationService();
97
98
        // This call will fail with a "NotFoundException", because the media
99
        // location was marked as trashed in the main storage
100
        $locationService->loadLocationByRemoteId($mediaRemoteId);
101
        /* END: Use Case */
102
    }
103
104
    /**
105
     * Test for the trash() method.
106
     *
107
     * @see \eZ\Publish\API\Repository\TrashService::trash()
108
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
109
     */
110
    public function testTrashRemovesChildLocationsFromMainStorage()
111
    {
112
        $repository = $this->getRepository();
113
114
        /* BEGIN: Use Case */
115
        $remoteIds = $this->createRemoteIdList();
116
117
        $this->createTrashItem();
118
119
        // All invocations to loadLocationByRemoteId() to one of the above
120
        // collected remoteIds will return in an "NotFoundException"
121
        /* END: Use Case */
122
123
        $locationService = $repository->getLocationService();
124
        foreach ($remoteIds as $remoteId) {
125
            try {
126
                $locationService->loadLocationByRemoteId($remoteId);
127
                $this->fail("Location '{$remoteId}' should exist.'");
128
            } catch (NotFoundException $e) {
129
                // echo $e->getFile(), ' +', $e->getLine(), PHP_EOL;
130
            }
131
        }
132
133
        $this->assertGreaterThan(
134
            0,
135
            count($remoteIds),
136
            "There should be at least one 'Community' child location."
137
        );
138
    }
139
140
    /**
141
     * Test for the trash() method.
142
     *
143
     * @see \eZ\Publish\API\Repository\TrashService::trash()
144
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
145
     */
146 View Code Duplication
    public function testTrashDecrementsChildCountOnParentLocation()
147
    {
148
        $repository = $this->getRepository();
149
        $locationService = $repository->getLocationService();
150
151
        $baseLocationId = $this->generateId('location', 1);
152
153
        $location = $locationService->loadLocation($baseLocationId);
154
155
        $childCount = $locationService->getLocationChildCount($location);
156
157
        $this->createTrashItem();
158
159
        $this->refreshSearch($repository);
160
161
        $this->assertEquals(
162
            $childCount - 1,
163
            $locationService->getLocationChildCount($location)
164
        );
165
    }
166
167
    /**
168
     * Test for the loadTrashItem() method.
169
     *
170
     * @see \eZ\Publish\API\Repository\TrashService::loadTrashItem()
171
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
172
     */
173
    public function testLoadTrashItem()
174
    {
175
        $repository = $this->getRepository();
176
        $trashService = $repository->getTrashService();
177
178
        /* BEGIN: Use Case */
179
        $trashItem = $this->createTrashItem();
180
181
        // Reload the trash item
182
        $trashItemReloaded = $trashService->loadTrashItem($trashItem->id);
183
        /* END: Use Case */
184
185
        $this->assertInstanceOf(
186
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\TrashItem',
187
            $trashItemReloaded
188
        );
189
190
        $this->assertEquals(
191
            $trashItem->pathString,
0 ignored issues
show
Documentation introduced by
The property $pathString is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
            $trashItemReloaded->pathString
0 ignored issues
show
Documentation introduced by
The property $pathString is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
193
        );
194
    }
195
196
    /**
197
     * Test for the loadTrashItem() method.
198
     *
199
     * @see \eZ\Publish\API\Repository\TrashService::loadTrashItem()
200
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
201
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testLoadTrashItem
202
     */
203
    public function testLoadTrashItemThrowsNotFoundException()
204
    {
205
        $repository = $this->getRepository();
206
207
        $nonExistingTrashId = $this->generateId('trash', 2342);
208
        /* BEGIN: Use Case */
209
        $trashService = $repository->getTrashService();
210
211
        // This call will fail with a "NotFoundException", because no trash item
212
        // with the ID 1342 should exist in an eZ Publish demo installation
213
        $trashService->loadTrashItem($nonExistingTrashId);
214
        /* END: Use Case */
215
    }
216
217
    /**
218
     * Test for the recover() method.
219
     *
220
     * @see \eZ\Publish\API\Repository\TrashService::recover()
221
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
222
     */
223
    public function testRecover()
224
    {
225
        $repository = $this->getRepository();
226
        $trashService = $repository->getTrashService();
227
        $locationService = $repository->getLocationService();
228
229
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
230
231
        /* BEGIN: Use Case */
232
        $trashItem = $this->createTrashItem();
233
234
        // Recover the trashed item
235
        $location = $trashService->recover($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 232 can be null; however, eZ\Publish\API\Repository\TrashService::recover() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
236
237
        // Load the recovered location
238
        $locationReloaded = $locationService->loadLocationByRemoteId(
239
            $mediaRemoteId
240
        );
241
        /* END: Use Case */
242
243
        $this->assertInstanceOf(
244
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
245
            $location
246
        );
247
248
        $this->assertEquals(
249
            $location->pathString,
250
            $locationReloaded->pathString
251
        );
252
253
        try {
254
            $trashService->loadTrashItem($trashItem->id);
255
            $this->fail('Trash item was not removed after being recovered.');
256
        } catch (NotFoundException $e) {
257
            // All well
258
        }
259
    }
260
261
    /**
262
     * Test for the recover() method.
263
     *
264
     * @see \eZ\Publish\API\Repository\TrashService::recover()
265
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
266
     */
267
    public function testRecoverDoesNotRestoreChildLocations()
268
    {
269
        $repository = $this->getRepository();
270
        $trashService = $repository->getTrashService();
271
        $locationService = $repository->getLocationService();
272
273
        $remoteIds = $this->createRemoteIdList();
274
275
        // Unset remote ID of actually restored location
276
        unset($remoteIds[array_search('3f6d92f8044aed134f32153517850f5a', $remoteIds)]);
277
278
        $trashItem = $this->createTrashItem();
279
280
        $trashService->recover($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 278 can be null; however, eZ\Publish\API\Repository\TrashService::recover() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
281
282
        $this->assertGreaterThan(
283
            0,
284
            count($remoteIds),
285
            "There should be at least one 'Community' child location."
286
        );
287
288
        // None of the child locations will be available again
289
        foreach ($remoteIds as $remoteId) {
290
            try {
291
                $locationService->loadLocationByRemoteId($remoteId);
292
                $this->fail(
293
                    sprintf(
294
                        'Location with remote ID "%s" unexpectedly restored.',
295
                        $remoteId
296
                    )
297
                );
298
            } catch (NotFoundException $e) {
299
                // All well
300
            }
301
        }
302
303
        try {
304
            $trashService->loadTrashItem($trashItem->id);
305
            $this->fail('Trash item was not removed after being recovered.');
306
        } catch (NotFoundException $e) {
307
            // All well
308
        }
309
    }
310
311
    /**
312
     * Test for the recover() method.
313
     *
314
     * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation)
315
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
316
     *
317
     * @todo Fix naming
318
     */
319
    public function testRecoverWithLocationCreateStructParameter()
320
    {
321
        $repository = $this->getRepository();
322
        $trashService = $repository->getTrashService();
323
        $locationService = $repository->getLocationService();
324
325
        $homeLocationId = $this->generateId('location', 2);
326
        /* BEGIN: Use Case */
327
        // $homeLocationId is the ID of the "Home" location in an eZ Publish
328
        // demo installation
329
330
        $trashItem = $this->createTrashItem();
331
332
        // Get the new parent location
333
        $newParentLocation = $locationService->loadLocation($homeLocationId);
334
335
        // Recover location with new location
336
        $location = $trashService->recover($trashItem, $newParentLocation);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 330 can be null; however, eZ\Publish\API\Repository\TrashService::recover() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
337
        /* END: Use Case */
338
339
        $this->assertPropertiesCorrect(
340
            array(
341
                'remoteId' => $trashItem->remoteId,
342
                'parentLocationId' => $homeLocationId,
343
                // Not the full sub tree is restored
344
                'depth' => $newParentLocation->depth + 1,
345
                'hidden' => false,
346
                'invisible' => $trashItem->invisible,
0 ignored issues
show
Documentation introduced by
The property $invisible is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
347
                'pathString' => $newParentLocation->pathString . $this->parseId('location', $location->id) . '/',
348
                'priority' => 0,
349
                'sortField' => Location::SORT_FIELD_NAME,
350
                'sortOrder' => Location::SORT_ORDER_ASC,
351
            ),
352
            $location
353
        );
354
355
        try {
356
            $trashService->loadTrashItem($trashItem->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
357
            $this->fail('Trash item was not removed after being recovered.');
358
        } catch (NotFoundException $e) {
359
            // All well
360
        }
361
    }
362
363
    /**
364
     * Test for the recover() method.
365
     *
366
     * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem)
367
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
368
     */
369
    public function testRecoverIncrementsChildCountOnOriginalParent()
370
    {
371
        $repository = $this->getRepository();
372
        $trashService = $repository->getTrashService();
373
        $locationService = $repository->getLocationService();
374
375
        $location = $locationService->loadLocation($this->generateId('location', 1));
376
377
        $trashItem = $this->createTrashItem();
378
379
        $this->refreshSearch($repository);
380
381
        /* BEGIN: Use Case */
382
        $childCount = $locationService->getLocationChildCount($location);
383
384
        // Recover location with new location
385
        $trashService->recover($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 377 can be null; however, eZ\Publish\API\Repository\TrashService::recover() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
386
        /* END: Use Case */
387
388
        $this->refreshSearch($repository);
389
390
        $this->assertEquals(
391
            $childCount + 1,
392
            $locationService->getLocationChildCount($location)
393
        );
394
395
        try {
396
            $trashService->loadTrashItem($trashItem->id);
397
            $this->fail('Trash item was not removed after being recovered.');
398
        } catch (NotFoundException $e) {
399
            // All well
400
        }
401
    }
402
403
    /**
404
     * Test for the recover() method.
405
     *
406
     * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation)
407
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecoverWithLocationCreateStructParameter
408
     */
409
    public function testRecoverWithLocationCreateStructParameterIncrementsChildCountOnNewParent()
410
    {
411
        $repository = $this->getRepository();
412
        $trashService = $repository->getTrashService();
413
        $locationService = $repository->getLocationService();
414
415
        $homeLocationId = $this->generateId('location', 2);
416
417
        $location = $locationService->loadLocation($homeLocationId);
418
419
        $childCount = $locationService->getLocationChildCount($location);
420
421
        /* BEGIN: Use Case */
422
        // $homeLocationId is the ID of the "Home" location in an eZ Publish
423
        // demo installation
424
425
        $trashItem = $this->createTrashItem();
426
427
        // Get the new parent location
428
        $newParentLocation = $locationService->loadLocation($homeLocationId);
429
430
        // Recover location with new location
431
        $trashService->recover($trashItem, $newParentLocation);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 425 can be null; however, eZ\Publish\API\Repository\TrashService::recover() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
432
        /* END: Use Case */
433
434
        $this->refreshSearch($repository);
435
436
        $this->assertEquals(
437
            $childCount + 1,
438
            $locationService->getLocationChildCount($location)
439
        );
440
441
        try {
442
            $trashService->loadTrashItem($trashItem->id);
443
            $this->fail('Trash item was not removed after being recovered.');
444
        } catch (NotFoundException $e) {
445
            // All well
446
        }
447
    }
448
449
    /**
450
     * Test for the findTrashItems() method.
451
     *
452
     * @see \eZ\Publish\API\Repository\TrashService::findTrashItems()
453
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
454
     */
455 View Code Duplication
    public function testFindTrashItems()
456
    {
457
        $repository = $this->getRepository();
458
        $trashService = $repository->getTrashService();
459
460
        /* BEGIN: Use Case */
461
        $this->createTrashItem();
462
463
        // Create a search query for all trashed items
464
        $query = new Query();
465
        $query->filter = new Criterion\LogicalAnd(
466
            array(
467
                new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
468
            )
469
        );
470
471
        // Load all trashed locations
472
        $searchResult = $trashService->findTrashItems($query);
473
        /* END: Use Case */
474
475
        $this->assertInstanceOf(
476
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\SearchResult',
477
            $searchResult
478
        );
479
480
        // 4 trashed locations from the sub tree
481
        $this->assertEquals(4, $searchResult->count);
482
    }
483
484
    /**
485
     * Test for the emptyTrash() method.
486
     *
487
     * @see \eZ\Publish\API\Repository\TrashService::emptyTrash()
488
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems
489
     */
490 View Code Duplication
    public function testEmptyTrash()
491
    {
492
        $repository = $this->getRepository();
493
        $trashService = $repository->getTrashService();
494
495
        /* BEGIN: Use Case */
496
        $this->createTrashItem();
497
498
        // Empty the trash
499
        $trashService->emptyTrash();
500
501
        // Create a search query for all trashed items
502
        $query = new Query();
503
        $query->filter = new Criterion\LogicalAnd(
504
            array(
505
                new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
506
            )
507
        );
508
509
        // Load all trashed locations, search result should be empty
510
        $searchResult = $trashService->findTrashItems($query);
511
        /* END: Use Case */
512
513
        $this->assertEquals(0, $searchResult->count);
514
    }
515
516
    /**
517
     * Test for the deleteTrashItem() method.
518
     *
519
     * @see \eZ\Publish\API\Repository\TrashService::deleteTrashItem()
520
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems
521
     */
522
    public function testDeleteTrashItem()
523
    {
524
        $repository = $this->getRepository();
525
        $trashService = $repository->getTrashService();
526
        $locationService = $repository->getLocationService();
527
528
        $demoDesignLocationId = $this->generateId('location', 56);
529
        /* BEGIN: Use Case */
530
        // $demoDesignLocationId is the ID of the "Demo Design" location in an eZ
531
        // Publish demo installation
532
533
        $trashItem = $this->createTrashItem();
534
535
        // Trash one more location
536
        $trashService->trash(
537
            $locationService->loadLocation($demoDesignLocationId)
538
        );
539
540
        // Empty the trash
541
        $trashService->deleteTrashItem($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 533 can be null; however, eZ\Publish\API\Repositor...vice::deleteTrashItem() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
542
543
        // Create a search query for all trashed items
544
        $query = new Query();
545
        $query->filter = new Criterion\LogicalAnd(
546
            array(
547
                new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
548
            )
549
        );
550
551
        // Load all trashed locations, should only contain the Demo Design location
552
        $searchResult = $trashService->findTrashItems($query);
553
        /* END: Use Case */
554
555
        $foundIds = array_map(
556
            function ($trashItem) {
557
                return $trashItem->id;
558
            },
559
            $searchResult->items
560
        );
561
562
        $this->assertEquals(4, $searchResult->count);
563
        $this->assertTrue(
564
            array_search($demoDesignLocationId, $foundIds) !== false
565
        );
566
    }
567
568
    /**
569
     * Returns an array with the remoteIds of all child locations of the
570
     * <b>Community</b> location. It is stored in a local variable named
571
     * <b>$remoteIds</b>.
572
     *
573
     * @return string[]
574
     */
575
    private function createRemoteIdList()
576
    {
577
        $repository = $this->getRepository();
578
579
        /* BEGIN: Inline */
580
        // remoteId of the "Community" location in an eZ Publish demo installation
581
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
582
583
        // Load the location service
584
        $locationService = $repository->getLocationService();
585
586
        $remoteIds = array();
587
        $children = $locationService->loadLocationChildren($locationService->loadLocationByRemoteId($mediaRemoteId));
588
        foreach ($children->locations as $child) {
589
            $remoteIds[] = $child->remoteId;
590
            foreach ($locationService->loadLocationChildren($child)->locations as $grandChild) {
591
                $remoteIds[] = $grandChild->remoteId;
592
            }
593
        }
594
        /* END: Inline */
595
596
        return $remoteIds;
597
    }
598
}
599