Completed
Push — travis_trusty ( e78e49...6f0d6f )
by André
54:54 queued 34:46
created

testRecoverWithLocationCreateStructParameter()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
nc 3
nop 0
dl 0
loc 43
rs 8.8571
c 0
b 0
f 0
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\Repository;
14
use eZ\Publish\API\Repository\URLAliasService;
15
use eZ\Publish\API\Repository\Values\Content\Location;
16
use eZ\Publish\API\Repository\Values\Content\Query;
17
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
18
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
19
20
/**
21
 * Test case for operations in the TrashService using in memory storage.
22
 *
23
 * @see eZ\Publish\API\Repository\TrashService
24
 * @group integration
25
 * @group trash
26
 */
27
class TrashServiceTest extends BaseTrashServiceTest
28
{
29
    /**
30
     * Test for the trash() method.
31
     *
32
     * @see \eZ\Publish\API\Repository\TrashService::trash()
33
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
34
     */
35
    public function testTrash()
36
    {
37
        /* BEGIN: Use Case */
38
        $trashItem = $this->createTrashItem();
39
        /* END: Use Case */
40
41
        $this->assertInstanceOf(
42
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\TrashItem',
43
            $trashItem
44
        );
45
    }
46
47
    /**
48
     * Test for the trash() method.
49
     *
50
     * @see \eZ\Publish\API\Repository\TrashService::trash()
51
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
52
     */
53
    public function testTrashSetsExpectedTrashItemProperties()
54
    {
55
        $repository = $this->getRepository();
56
57
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
58
59
        // Load the location that will be trashed
60
        $location = $repository->getLocationService()
61
            ->loadLocationByRemoteId($mediaRemoteId);
62
63
        $expected = array(
64
            'id' => $location->id,
65
            'depth' => $location->depth,
66
            'hidden' => $location->hidden,
67
            'invisible' => $location->invisible,
68
            'parentLocationId' => $location->parentLocationId,
69
            'pathString' => $location->pathString,
70
            'priority' => $location->priority,
71
            'remoteId' => $location->remoteId,
72
            'sortField' => $location->sortField,
73
            'sortOrder' => $location->sortOrder,
74
        );
75
76
        $trashItem = $this->createTrashItem();
77
78
        $this->assertPropertiesCorrect($expected, $trashItem);
79
    }
80
81
    /**
82
     * Test for the trash() method.
83
     *
84
     * @see \eZ\Publish\API\Repository\TrashService::trash()
85
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
86
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
87
     */
88
    public function testTrashRemovesLocationFromMainStorage()
89
    {
90
        $repository = $this->getRepository();
91
92
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
93
94
        /* BEGIN: Use Case */
95
        $this->createTrashItem();
96
97
        // Load the location service
98
        $locationService = $repository->getLocationService();
99
100
        // This call will fail with a "NotFoundException", because the media
101
        // location was marked as trashed in the main storage
102
        $locationService->loadLocationByRemoteId($mediaRemoteId);
103
        /* END: Use Case */
104
    }
105
106
    /**
107
     * Test for the trash() method.
108
     *
109
     * @see \eZ\Publish\API\Repository\TrashService::trash()
110
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
111
     */
112
    public function testTrashRemovesChildLocationsFromMainStorage()
113
    {
114
        $repository = $this->getRepository();
115
116
        /* BEGIN: Use Case */
117
        $remoteIds = $this->createRemoteIdList();
118
119
        $this->createTrashItem();
120
121
        // All invocations to loadLocationByRemoteId() to one of the above
122
        // collected remoteIds will return in an "NotFoundException"
123
        /* END: Use Case */
124
125
        $locationService = $repository->getLocationService();
126
        foreach ($remoteIds as $remoteId) {
127
            try {
128
                $locationService->loadLocationByRemoteId($remoteId);
129
                $this->fail("Location '{$remoteId}' should exist.'");
130
            } catch (NotFoundException $e) {
131
                // echo $e->getFile(), ' +', $e->getLine(), PHP_EOL;
132
            }
133
        }
134
135
        $this->assertGreaterThan(
136
            0,
137
            count($remoteIds),
138
            "There should be at least one 'Community' child location."
139
        );
140
    }
141
142
    /**
143
     * Test for the trash() method.
144
     *
145
     * @see \eZ\Publish\API\Repository\TrashService::trash()
146
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
147
     */
148 View Code Duplication
    public function testTrashDecrementsChildCountOnParentLocation()
149
    {
150
        $repository = $this->getRepository();
151
        $locationService = $repository->getLocationService();
152
153
        $baseLocationId = $this->generateId('location', 1);
154
155
        $location = $locationService->loadLocation($baseLocationId);
156
157
        $childCount = $locationService->getLocationChildCount($location);
158
159
        $this->createTrashItem();
160
161
        $this->refreshSearch($repository);
162
163
        $this->assertEquals(
164
            $childCount - 1,
165
            $locationService->getLocationChildCount($location)
166
        );
167
    }
168
169
    /**
170
     * Test for the loadTrashItem() method.
171
     *
172
     * @see \eZ\Publish\API\Repository\TrashService::loadTrashItem()
173
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
174
     */
175
    public function testLoadTrashItem()
176
    {
177
        $repository = $this->getRepository();
178
        $trashService = $repository->getTrashService();
179
180
        /* BEGIN: Use Case */
181
        $trashItem = $this->createTrashItem();
182
183
        // Reload the trash item
184
        $trashItemReloaded = $trashService->loadTrashItem($trashItem->id);
185
        /* END: Use Case */
186
187
        $this->assertInstanceOf(
188
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\TrashItem',
189
            $trashItemReloaded
190
        );
191
192
        $this->assertEquals(
193
            $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...
194
            $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...
195
        );
196
    }
197
198
    /**
199
     * Test for the loadTrashItem() method.
200
     *
201
     * @see \eZ\Publish\API\Repository\TrashService::loadTrashItem()
202
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
203
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testLoadTrashItem
204
     */
205
    public function testLoadTrashItemThrowsNotFoundException()
206
    {
207
        $repository = $this->getRepository();
208
209
        $nonExistingTrashId = $this->generateId('trash', 2342);
210
        /* BEGIN: Use Case */
211
        $trashService = $repository->getTrashService();
212
213
        // This call will fail with a "NotFoundException", because no trash item
214
        // with the ID 1342 should exist in an eZ Publish demo installation
215
        $trashService->loadTrashItem($nonExistingTrashId);
216
        /* END: Use Case */
217
    }
218
219
    /**
220
     * Test for the recover() method.
221
     *
222
     * @see \eZ\Publish\API\Repository\TrashService::recover()
223
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
224
     */
225
    public function testRecover()
226
    {
227
        $repository = $this->getRepository();
228
        $trashService = $repository->getTrashService();
229
        $locationService = $repository->getLocationService();
230
231
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
232
233
        /* BEGIN: Use Case */
234
        $trashItem = $this->createTrashItem();
235
236
        // Recover the trashed item
237
        $location = $trashService->recover($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 234 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...
238
239
        // Load the recovered location
240
        $locationReloaded = $locationService->loadLocationByRemoteId(
241
            $mediaRemoteId
242
        );
243
        /* END: Use Case */
244
245
        $this->assertInstanceOf(
246
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
247
            $location
248
        );
249
250
        $this->assertEquals(
251
            $location->pathString,
252
            $locationReloaded->pathString
253
        );
254
255
        try {
256
            $trashService->loadTrashItem($trashItem->id);
257
            $this->fail('Trash item was not removed after being recovered.');
258
        } catch (NotFoundException $e) {
259
            // All well
260
        }
261
    }
262
263
    /**
264
     * Test for the trash() method.
265
     *
266
     * @see \eZ\Publish\API\Repository\TrashService::recover()
267
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
268
     *
269
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
270
     */
271 View Code Duplication
    public function testNotFoundAliasAfterRemoveIt()
272
    {
273
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
274
275
        $repository = $this->getRepository();
276
        $trashService = $repository->getTrashService();
277
        $urlAliasService = $repository->getURLAliasService();
278
        $locationService = $repository->getLocationService();
279
280
        // Double ->lookup() call because there where issue that one call was not enough to spot bug
281
        $urlAliasService->lookup('/Media');
282
        $urlAliasService->lookup('/Media');
283
284
        $mediaLocation = $locationService->loadLocationByRemoteId($mediaRemoteId);
285
        $trashService->trash($mediaLocation);
286
287
        $urlAliasService->lookup('/Media');
288
    }
289
290
    /**
291
     * Test for the recover() method.
292
     *
293
     * @see \eZ\Publish\API\Repository\TrashService::recover()
294
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
295
     */
296
    public function testAliasesForRemovedItems()
297
    {
298
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
299
300
        $repository = $this->getRepository();
301
        $trashService = $repository->getTrashService();
302
        $urlAliasService = $repository->getURLAliasService();
303
        $locationService = $repository->getLocationService();
304
305
        // Double ->lookup() call because there where issue that one call was not enough to spot bug
306
        $urlAliasService->lookup('/Media');
307
        $trashedLocationAlias = $urlAliasService->lookup('/Media');
308
309
        $mediaLocation = $locationService->loadLocationByRemoteId($mediaRemoteId);
310
        $trashItem = $trashService->trash($mediaLocation);
311
        $this->assertAliasNotExists($urlAliasService, '/Media');
312
313
        $this->createNewContentInPlaceTrashedOne($repository, $mediaLocation->parentLocationId);
314
315
        $createdLocationAlias = $urlAliasService->lookup('/Media');
316
317
        $this->assertNotEquals(
318
            $trashedLocationAlias->destination,
319
            $createdLocationAlias->destination,
320
            'Destination for /media url should changed'
321
        );
322
323
        $recoveredLocation = $trashService->recover($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $trashService->trash($mediaLocation) on line 310 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...
324
        $recoveredLocationAlias = $urlAliasService->lookup('/Media2');
325
        $recoveredLocationAliasReverse = $urlAliasService->reverseLookup($recoveredLocation);
326
327
        $this->assertEquals($recoveredLocationAlias->destination, $recoveredLocationAliasReverse->destination);
328
329
        $this->assertNotEquals($recoveredLocationAliasReverse->destination, $trashedLocationAlias->destination);
330
        $this->assertNotEquals($recoveredLocationAliasReverse->destination, $createdLocationAlias->destination);
331
    }
332
333
    /**
334
     * Test for the recover() method.
335
     *
336
     * @see \eZ\Publish\API\Repository\TrashService::recover()
337
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
338
     */
339
    public function testRecoverDoesNotRestoreChildLocations()
340
    {
341
        $repository = $this->getRepository();
342
        $trashService = $repository->getTrashService();
343
        $locationService = $repository->getLocationService();
344
345
        $remoteIds = $this->createRemoteIdList();
346
347
        // Unset remote ID of actually restored location
348
        unset($remoteIds[array_search('3f6d92f8044aed134f32153517850f5a', $remoteIds)]);
349
350
        $trashItem = $this->createTrashItem();
351
352
        $trashService->recover($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 350 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...
353
354
        $this->assertGreaterThan(
355
            0,
356
            count($remoteIds),
357
            "There should be at least one 'Community' child location."
358
        );
359
360
        // None of the child locations will be available again
361
        foreach ($remoteIds as $remoteId) {
362
            try {
363
                $locationService->loadLocationByRemoteId($remoteId);
364
                $this->fail(
365
                    sprintf(
366
                        'Location with remote ID "%s" unexpectedly restored.',
367
                        $remoteId
368
                    )
369
                );
370
            } catch (NotFoundException $e) {
371
                // All well
372
            }
373
        }
374
375
        try {
376
            $trashService->loadTrashItem($trashItem->id);
377
            $this->fail('Trash item was not removed after being recovered.');
378
        } catch (NotFoundException $e) {
379
            // All well
380
        }
381
    }
382
383
    /**
384
     * Test for the recover() method.
385
     *
386
     * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation)
387
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
388
     *
389
     * @todo Fix naming
390
     */
391
    public function testRecoverWithLocationCreateStructParameter()
392
    {
393
        $repository = $this->getRepository();
394
        $trashService = $repository->getTrashService();
395
        $locationService = $repository->getLocationService();
396
397
        $homeLocationId = $this->generateId('location', 2);
398
        /* BEGIN: Use Case */
399
        // $homeLocationId is the ID of the "Home" location in an eZ Publish
400
        // demo installation
401
402
        $trashItem = $this->createTrashItem();
403
404
        // Get the new parent location
405
        $newParentLocation = $locationService->loadLocation($homeLocationId);
406
407
        // Recover location with new location
408
        $location = $trashService->recover($trashItem, $newParentLocation);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 402 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...
409
        /* END: Use Case */
410
411
        $this->assertPropertiesCorrect(
412
            array(
413
                'remoteId' => $trashItem->remoteId,
414
                'parentLocationId' => $homeLocationId,
415
                // Not the full sub tree is restored
416
                'depth' => $newParentLocation->depth + 1,
417
                'hidden' => false,
418
                '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...
419
                'pathString' => $newParentLocation->pathString . $this->parseId('location', $location->id) . '/',
420
                'priority' => 0,
421
                'sortField' => Location::SORT_FIELD_NAME,
422
                'sortOrder' => Location::SORT_ORDER_ASC,
423
            ),
424
            $location
425
        );
426
427
        try {
428
            $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...
429
            $this->fail('Trash item was not removed after being recovered.');
430
        } catch (NotFoundException $e) {
431
            // All well
432
        }
433
    }
434
435
    /**
436
     * Test for the recover() method.
437
     *
438
     * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem)
439
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover
440
     */
441
    public function testRecoverIncrementsChildCountOnOriginalParent()
442
    {
443
        $repository = $this->getRepository();
444
        $trashService = $repository->getTrashService();
445
        $locationService = $repository->getLocationService();
446
447
        $location = $locationService->loadLocation($this->generateId('location', 1));
448
449
        $trashItem = $this->createTrashItem();
450
451
        $this->refreshSearch($repository);
452
453
        /* BEGIN: Use Case */
454
        $childCount = $locationService->getLocationChildCount($location);
455
456
        // Recover location with new location
457
        $trashService->recover($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 449 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...
458
        /* END: Use Case */
459
460
        $this->refreshSearch($repository);
461
462
        $this->assertEquals(
463
            $childCount + 1,
464
            $locationService->getLocationChildCount($location)
465
        );
466
467
        try {
468
            $trashService->loadTrashItem($trashItem->id);
469
            $this->fail('Trash item was not removed after being recovered.');
470
        } catch (NotFoundException $e) {
471
            // All well
472
        }
473
    }
474
475
    /**
476
     * Test for the recover() method.
477
     *
478
     * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation)
479
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecoverWithLocationCreateStructParameter
480
     */
481
    public function testRecoverWithLocationCreateStructParameterIncrementsChildCountOnNewParent()
482
    {
483
        $repository = $this->getRepository();
484
        $trashService = $repository->getTrashService();
485
        $locationService = $repository->getLocationService();
486
487
        $homeLocationId = $this->generateId('location', 2);
488
489
        $location = $locationService->loadLocation($homeLocationId);
490
491
        $childCount = $locationService->getLocationChildCount($location);
492
493
        /* BEGIN: Use Case */
494
        // $homeLocationId is the ID of the "Home" location in an eZ Publish
495
        // demo installation
496
497
        $trashItem = $this->createTrashItem();
498
499
        // Get the new parent location
500
        $newParentLocation = $locationService->loadLocation($homeLocationId);
501
502
        // Recover location with new location
503
        $trashService->recover($trashItem, $newParentLocation);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 497 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...
504
        /* END: Use Case */
505
506
        $this->refreshSearch($repository);
507
508
        $this->assertEquals(
509
            $childCount + 1,
510
            $locationService->getLocationChildCount($location)
511
        );
512
513
        try {
514
            $trashService->loadTrashItem($trashItem->id);
515
            $this->fail('Trash item was not removed after being recovered.');
516
        } catch (NotFoundException $e) {
517
            // All well
518
        }
519
    }
520
521
    /**
522
     * Test for the findTrashItems() method.
523
     *
524
     * @see \eZ\Publish\API\Repository\TrashService::findTrashItems()
525
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash
526
     */
527 View Code Duplication
    public function testFindTrashItems()
528
    {
529
        $repository = $this->getRepository();
530
        $trashService = $repository->getTrashService();
531
532
        /* BEGIN: Use Case */
533
        $this->createTrashItem();
534
535
        // Create a search query for all trashed items
536
        $query = new Query();
537
        $query->filter = new Criterion\LogicalAnd(
538
            array(
539
                new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
540
            )
541
        );
542
543
        // Load all trashed locations
544
        $searchResult = $trashService->findTrashItems($query);
545
        /* END: Use Case */
546
547
        $this->assertInstanceOf(
548
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\SearchResult',
549
            $searchResult
550
        );
551
552
        // 4 trashed locations from the sub tree
553
        $this->assertEquals(4, $searchResult->count);
554
    }
555
556
    /**
557
     * Test for the emptyTrash() method.
558
     *
559
     * @see \eZ\Publish\API\Repository\TrashService::emptyTrash()
560
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems
561
     */
562 View Code Duplication
    public function testEmptyTrash()
563
    {
564
        $repository = $this->getRepository();
565
        $trashService = $repository->getTrashService();
566
567
        /* BEGIN: Use Case */
568
        $this->createTrashItem();
569
570
        // Empty the trash
571
        $trashService->emptyTrash();
572
573
        // Create a search query for all trashed items
574
        $query = new Query();
575
        $query->filter = new Criterion\LogicalAnd(
576
            array(
577
                new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
578
            )
579
        );
580
581
        // Load all trashed locations, search result should be empty
582
        $searchResult = $trashService->findTrashItems($query);
583
        /* END: Use Case */
584
585
        $this->assertEquals(0, $searchResult->count);
586
    }
587
588
    /**
589
     * Test for the deleteTrashItem() method.
590
     *
591
     * @see \eZ\Publish\API\Repository\TrashService::deleteTrashItem()
592
     * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems
593
     */
594
    public function testDeleteTrashItem()
595
    {
596
        $repository = $this->getRepository();
597
        $trashService = $repository->getTrashService();
598
        $locationService = $repository->getLocationService();
599
600
        $demoDesignLocationId = $this->generateId('location', 56);
601
        /* BEGIN: Use Case */
602
        // $demoDesignLocationId is the ID of the "Demo Design" location in an eZ
603
        // Publish demo installation
604
605
        $trashItem = $this->createTrashItem();
606
607
        // Trash one more location
608
        $trashService->trash(
609
            $locationService->loadLocation($demoDesignLocationId)
610
        );
611
612
        // Empty the trash
613
        $trashService->deleteTrashItem($trashItem);
0 ignored issues
show
Bug introduced by
It seems like $trashItem defined by $this->createTrashItem() on line 605 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...
614
615
        // Create a search query for all trashed items
616
        $query = new Query();
617
        $query->filter = new Criterion\LogicalAnd(
618
            array(
619
                new Criterion\Field('title', Criterion\Operator::LIKE, '*'),
620
            )
621
        );
622
623
        // Load all trashed locations, should only contain the Demo Design location
624
        $searchResult = $trashService->findTrashItems($query);
625
        /* END: Use Case */
626
627
        $foundIds = array_map(
628
            function ($trashItem) {
629
                return $trashItem->id;
630
            },
631
            $searchResult->items
632
        );
633
634
        $this->assertEquals(4, $searchResult->count);
635
        $this->assertTrue(
636
            array_search($demoDesignLocationId, $foundIds) !== false
637
        );
638
    }
639
640
    /**
641
     * Returns an array with the remoteIds of all child locations of the
642
     * <b>Community</b> location. It is stored in a local variable named
643
     * <b>$remoteIds</b>.
644
     *
645
     * @return string[]
646
     */
647
    private function createRemoteIdList()
648
    {
649
        $repository = $this->getRepository();
650
651
        /* BEGIN: Inline */
652
        // remoteId of the "Community" location in an eZ Publish demo installation
653
        $mediaRemoteId = '75c715a51699d2d309a924eca6a95145';
654
655
        // Load the location service
656
        $locationService = $repository->getLocationService();
657
658
        $remoteIds = array();
659
        $children = $locationService->loadLocationChildren($locationService->loadLocationByRemoteId($mediaRemoteId));
660
        foreach ($children->locations as $child) {
661
            $remoteIds[] = $child->remoteId;
662
            foreach ($locationService->loadLocationChildren($child)->locations as $grandChild) {
663
                $remoteIds[] = $grandChild->remoteId;
664
            }
665
        }
666
        /* END: Inline */
667
668
        return $remoteIds;
669
    }
670
671
    /**
672
     * @param Repository $repository
673
     * @param int $parentLocationId
674
     *
675
     * @return \eZ\Publish\API\Repository\Values\Content\Content
676
     */
677
    protected function createNewContentInPlaceTrashedOne(Repository $repository, $parentLocationId)
678
    {
679
        $contentService = $repository->getContentService();
680
        $locationService = $repository->getLocationService();
681
        $contentTypeService = $repository->getContentTypeService();
682
683
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
684
        $newContent = $contentService->newContentCreateStruct($contentType, 'eng-US');
685
        $newContent->setField('name', 'Media');
686
687
        $location = $locationService->newLocationCreateStruct($parentLocationId);
688
689
        $draftContent = $contentService->createContent($newContent, [$location]);
690
691
        return $contentService->publishVersion($draftContent->versionInfo);
692
    }
693
694
    /**
695
     * @param URLAliasService $urlAliasService
696
     * @param string $urlPath Url alias path
697
     *
698
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
699
     */
700
    private function assertAliasExists(URLAliasService $urlAliasService, $urlPath)
701
    {
702
        $urlAlias = $urlAliasService->lookup($urlPath);
703
704
        $this->assertInstanceOf('\eZ\Publish\API\Repository\Values\Content\URLAlias', $urlAlias);
705
706
        return $urlAlias;
707
    }
708
709
    /**
710
     * @param URLAliasService $urlAliasService
711
     * @param string $urlPath Url alias path
712
     */
713
    private function assertAliasNotExists(URLAliasService $urlAliasService, $urlPath)
0 ignored issues
show
Unused Code introduced by
The parameter $urlAliasService is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
714
    {
715
        try {
716
            $this->getRepository()->getURLAliasService()->lookup($urlPath);
717
            $this->fail(sprintf('Alias [%s] should not exists', $urlPath));
718
        } catch (\eZ\Publish\API\Repository\Exceptions\NotFoundException $e) {
719
            $this->assertTrue(true);
720
        }
721
    }
722
}
723