Completed
Push — ezp_30300 ( ef176f...d1dcf2 )
by
unknown
26:24
created

LocationHandlerTest::testMarkSubtreeModified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 25
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\Content\LocationHandlerTest 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\Core\Persistence\Legacy\Tests\Content;
10
11
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase;
12
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler;
13
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway;
14
use eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct;
15
use eZ\Publish\SPI\Persistence\Content\Location\CreateStruct;
16
use eZ\Publish\SPI\Persistence\Content\Location;
17
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
18
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
19
use eZ\Publish\SPI\Persistence\Content;
20
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper;
21
use eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler;
22
use eZ\Publish\Core\Persistence\Legacy\Content\Handler as ContentHandler;
23
use eZ\Publish\SPI\Persistence\Content\ObjectState;
24
use eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler as ObjectStateHandler;
25
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group as ObjectStateGroup;
26
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler as LocationHandler;
27
28
/**
29
 * Test case for LocationHandlerTest.
30
 */
31
class LocationHandlerTest extends TestCase
32
{
33
    /**
34
     * Mocked location gateway instance.
35
     *
36
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway
37
     */
38
    protected $locationGateway;
39
40
    /**
41
     * Mocked location mapper instance.
42
     *
43
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper
44
     */
45
    protected $locationMapper;
46
47
    /**
48
     * Mocked content handler instance.
49
     *
50
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Handler
51
     */
52
    protected $contentHandler;
53
54
    /**
55
     * Mocked object state handler instance.
56
     *
57
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler|\PHPUnit\Framework\MockObject\MockObject
58
     */
59
    protected $objectStateHandler;
60
61
    /**
62
     * Mocked Tree handler instance.
63
     *
64
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler|\PHPUnit\Framework\MockObject\MockObject
65
     */
66
    protected $treeHandler;
67
68 View Code Duplication
    public function setUp()
69
    {
70
        parent::setUp();
71
72
        $this->locationGateway = $this->createMock(Gateway::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...ocation\Gateway::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\P...ntent\Location\Gateway> of property $locationGateway.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
        $this->locationMapper = $this->createMock(Mapper::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...Location\Mapper::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\P...ontent\Location\Mapper> of property $locationMapper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
        $this->treeHandler = $this->createMock(TreeHandler::class);
75
        $this->contentHandler = $this->createMock(ContentHandler::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...Content\Handler::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\P...Legacy\Content\Handler> of property $contentHandler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
    }
77
78
    protected function getLocationHandler()
79
    {
80
        $dbHandler = $this->getDatabaseHandler();
0 ignored issues
show
Unused Code introduced by
$dbHandler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
82
        return new Handler(
83
            $this->locationGateway,
84
            $this->locationMapper,
85
            $this->contentHandler,
86
            $this->createMock(ObjectStateHandler::class),
87
            $this->treeHandler
0 ignored issues
show
Bug introduced by
It seems like $this->treeHandler can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Persiste...\Handler::__construct() does only seem to accept object<eZ\Publish\Core\P...cy\Content\TreeHandler>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88
        );
89
    }
90
91
    public function testLoadLocation()
92
    {
93
        $handler = $this->getLocationHandler();
94
95
        $this->treeHandler
96
            ->expects($this->once())
97
            ->method('loadLocation')
98
            ->with(77)
99
            ->will($this->returnValue(new \eZ\Publish\SPI\Persistence\Content\Location()));
100
101
        $location = $handler->load(77);
102
103
        $this->assertTrue($location instanceof \eZ\Publish\SPI\Persistence\Content\Location);
104
    }
105
106
    public function testLoadLocationSubtree()
107
    {
108
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
            ->expects($this->once())
110
            ->method('getSubtreeContent')
111
            ->with(77, true)
112
            ->will(
113
                $this->returnValue(
114
                    array(
115
                        array(77 => 100),
116
                        array(78 => 101),
117
                    )
118
                )
119
            );
120
121
        $this->assertEquals(2, count($this->getLocationHandler()->loadSubtreeIds(77)));
122
    }
123
124 View Code Duplication
    public function testLoadLocationByRemoteId()
125
    {
126
        $handler = $this->getLocationHandler();
127
128
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
            ->expects($this->once())
130
            ->method('getBasicNodeDataByRemoteId')
131
            ->with('abc123')
132
            ->will(
133
                $this->returnValue(
134
                    array(
135
                        'node_id' => 77,
136
                    )
137
                )
138
            );
139
140
        $this->locationMapper
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ontent\Location\Mapper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
            ->expects($this->once())
142
            ->method('createLocationFromRow')
143
            ->with(array('node_id' => 77))
144
            ->will($this->returnValue(new \eZ\Publish\SPI\Persistence\Content\Location()));
145
146
        $location = $handler->loadByRemoteId('abc123');
147
148
        $this->assertTrue($location instanceof \eZ\Publish\SPI\Persistence\Content\Location);
149
    }
150
151 View Code Duplication
    public function testLoadLocationsByContent()
152
    {
153
        $handler = $this->getLocationHandler();
154
155
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
            ->expects($this->once())
157
            ->method('loadLocationDataByContent')
158
            ->with(23, 42)
159
            ->will(
160
                $this->returnValue(
161
                    array()
162
                )
163
            );
164
165
        $this->locationMapper
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ontent\Location\Mapper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
            ->expects($this->once())
167
            ->method('createLocationsFromRows')
168
            ->with(array())
169
            ->will($this->returnValue(array('a', 'b')));
170
171
        $locations = $handler->loadLocationsByContent(23, 42);
172
173
        $this->assertInternalType('array', $locations);
174
    }
175
176 View Code Duplication
    public function loadParentLocationsForDraftContent()
177
    {
178
        $handler = $this->getLocationHandler();
179
180
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
            ->expects($this->once())
182
            ->method('loadParentLocationsDataForDraftContent')
183
            ->with(23)
184
            ->will(
185
                $this->returnValue(
186
                    array()
187
                )
188
            );
189
190
        $this->locationMapper
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ontent\Location\Mapper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
            ->expects($this->once())
192
            ->method('createLocationsFromRows')
193
            ->with(array())
194
            ->will($this->returnValue(array('a', 'b')));
195
196
        $locations = $handler->loadParentLocationsForDraftContent(23);
197
198
        $this->assertInternalType('array', $locations);
199
    }
200
201
    public function testMoveSubtree()
202
    {
203
        $handler = $this->getLocationHandler();
204
205
        $sourceData = array(
206
            'node_id' => 69,
207
            'path_string' => '/1/2/69/',
208
            'parent_node_id' => 2,
209
            'contentobject_id' => 67,
210
        );
211
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
            ->expects($this->at(0))
213
            ->method('getBasicNodeData')
214
            ->with(69)
215
            ->will($this->returnValue($sourceData));
216
217
        $destinationData = array(
218
            'node_id' => 77,
219
            'path_string' => '/1/2/77/',
220
            'contentobject_id' => 68,
221
        );
222
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
223
            ->expects($this->at(1))
224
            ->method('getBasicNodeData')
225
            ->with(77)
226
            ->will($this->returnValue($destinationData));
227
228
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
229
            ->expects($this->once())
230
            ->method('moveSubtreeNodes')
231
            ->with($sourceData, $destinationData);
232
233
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
234
            ->expects($this->once())
235
            ->method('updateNodeAssignment')
236
            ->with(67, 2, 77, 5);
237
238
        $this->treeHandler
239
            ->expects($this->at(0))
240
            ->method('loadLocation')
241
            ->with($sourceData['node_id'])
242
            ->will($this->returnValue(
243
                new Location(
244
                    array(
245
                        'id' => $sourceData['node_id'],
246
                        'contentId' => $sourceData['contentobject_id'],
247
                    )
248
                )
249
            ));
250
251
        $this->treeHandler
252
            ->expects($this->at(1))
253
            ->method('loadLocation')
254
            ->with($destinationData['node_id'])
255
            ->will($this->returnValue(new Location(array('contentId' => $destinationData['contentobject_id']))));
256
257
        $this->contentHandler
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...Legacy\Content\Handler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
258
            ->expects($this->at(0))
259
            ->method('loadContentInfo')
260
            ->with($destinationData['contentobject_id'])
261
            ->will($this->returnValue(new ContentInfo(array('sectionId' => 12345))));
262
263
        $this->contentHandler
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...Legacy\Content\Handler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
264
            ->expects($this->at(1))
265
            ->method('loadContentInfo')
266
            ->with($sourceData['contentobject_id'])
267
            ->will($this->returnValue(new ContentInfo(array('mainLocationId' => 69))));
268
269
        $this->treeHandler
270
            ->expects($this->once())
271
            ->method('setSectionForSubtree')
272
            ->with(69, 12345);
273
274
        $handler->move(69, 77);
275
    }
276
277
    public function testHideUpdateHidden()
278
    {
279
        $handler = $this->getLocationHandler();
280
281
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
282
            ->expects($this->at(0))
283
            ->method('getBasicNodeData')
284
            ->with(69)
285
            ->will(
286
                $this->returnValue(
287
                    array(
288
                        'node_id' => 69,
289
                        'path_string' => '/1/2/69/',
290
                        'contentobject_id' => 67,
291
                    )
292
                )
293
            );
294
295
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
296
            ->expects($this->once())
297
            ->method('hideSubtree')
298
            ->with('/1/2/69/');
299
300
        $handler->hide(69);
301
    }
302
303
    /**
304
     * @depends testHideUpdateHidden
305
     */
306
    public function testHideUnhideUpdateHidden()
307
    {
308
        $handler = $this->getLocationHandler();
309
310
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
311
            ->expects($this->at(0))
312
            ->method('getBasicNodeData')
313
            ->with(69)
314
            ->will(
315
                $this->returnValue(
316
                    array(
317
                        'node_id' => 69,
318
                        'path_string' => '/1/2/69/',
319
                        'contentobject_id' => 67,
320
                    )
321
                )
322
            );
323
324
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
325
            ->expects($this->once())
326
            ->method('unhideSubtree')
327
            ->with('/1/2/69/');
328
329
        $handler->unhide(69);
330
    }
331
332
    public function testSwapLocations()
333
    {
334
        $handler = $this->getLocationHandler();
335
336
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
337
            ->expects($this->once())
338
            ->method('swap')
339
            ->with(70, 78);
340
341
        $handler->swap(70, 78);
342
    }
343
344
    public function testCreateLocation()
345
    {
346
        $handler = $this->getLocationHandler();
347
348
        $createStruct = new CreateStruct();
349
        $createStruct->parentId = 77;
350
351
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
352
            ->expects($this->once())
353
            ->method('getBasicNodeData')
354
            ->with(77)
355
            ->will(
356
                $this->returnValue(
357
                    $parentInfo = array(
358
                        'node_id' => 77,
359
                        'path_string' => '/1/2/77/',
360
                    )
361
                )
362
            );
363
364
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
365
            ->expects($this->once())
366
            ->method('create')
367
            ->with($createStruct, $parentInfo)
368
            ->will($this->returnValue($createStruct));
369
370
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
371
            ->expects($this->once())
372
            ->method('createNodeAssignment')
373
            ->with($createStruct, 77, 2);
374
375
        $handler->create($createStruct);
376
    }
377
378
    public function testUpdateLocation()
379
    {
380
        $handler = $this->getLocationHandler();
381
382
        $updateStruct = new UpdateStruct();
383
        $updateStruct->priority = 77;
384
385
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
386
            ->expects($this->once())
387
            ->method('update')
388
            ->with($updateStruct, 23);
389
390
        $handler->update($updateStruct, 23);
391
    }
392
393
    public function testSetSectionForSubtree()
394
    {
395
        $handler = $this->getLocationHandler();
396
397
        $this->treeHandler
398
            ->expects($this->once())
399
            ->method('setSectionForSubtree')
400
            ->with(69, 3);
401
402
        $handler->setSectionForSubtree(69, 3);
403
    }
404
405 View Code Duplication
    public function testMarkSubtreeModified()
406
    {
407
        $handler = $this->getLocationHandler();
408
409
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
410
            ->expects($this->at(0))
411
            ->method('getBasicNodeData')
412
            ->with(69)
413
            ->will(
414
                $this->returnValue(
415
                    array(
416
                        'node_id' => 69,
417
                        'path_string' => '/1/2/69/',
418
                        'contentobject_id' => 67,
419
                    )
420
                )
421
            );
422
423
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
424
            ->expects($this->at(1))
425
            ->method('updateSubtreeModificationTime')
426
            ->with('/1/2/69/');
427
428
        $handler->markSubtreeModified(69);
429
    }
430
431
    /**
432
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler::changeMainLocation
433
     */
434
    public function testChangeMainLocation()
435
    {
436
        $handler = $this->getLocationHandler();
437
438
        $this->treeHandler
439
            ->expects($this->once())
440
            ->method('changeMainLocation')
441
            ->with(12, 34);
442
443
        $handler->changeMainLocation(12, 34);
444
    }
445
446
    /**
447
     * Test for the removeSubtree() method.
448
     *
449
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler::removeSubtree
450
     */
451
    public function testRemoveSubtree()
452
    {
453
        $handler = $this->getLocationHandler();
454
455
        $this->treeHandler
456
            ->expects($this->once())
457
            ->method('removeSubtree')
458
            ->with(42);
459
460
        $handler->removeSubtree(42);
461
    }
462
463
    /**
464
     * Test for the copySubtree() method.
465
     *
466
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler::copySubtree
467
     */
468
    public function testCopySubtree()
469
    {
470
        $handler = $this->getPartlyMockedHandler(
471
            array(
472
                'load',
473
                'changeMainLocation',
474
                'setSectionForSubtree',
475
                'create',
476
            )
477
        );
478
        $subtreeContentRows = array(
479
            array('node_id' => 10, 'main_node_id' => 1, 'parent_node_id' => 3, 'contentobject_id' => 21, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_10', 'sort_field' => 2, 'sort_order' => 1),
480
            array('node_id' => 11, 'main_node_id' => 11, 'parent_node_id' => 10, 'contentobject_id' => 211, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_11', 'sort_field' => 2, 'sort_order' => 1),
481
            array('node_id' => 12, 'main_node_id' => 15, 'parent_node_id' => 10, 'contentobject_id' => 215, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_12', 'sort_field' => 2, 'sort_order' => 1),
482
            array('node_id' => 13, 'main_node_id' => 2, 'parent_node_id' => 10, 'contentobject_id' => 22, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_13', 'sort_field' => 2, 'sort_order' => 1),
483
            array('node_id' => 14, 'main_node_id' => 11, 'parent_node_id' => 13, 'contentobject_id' => 211, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_14', 'sort_field' => 2, 'sort_order' => 1),
484
            array('node_id' => 15, 'main_node_id' => 15, 'parent_node_id' => 13, 'contentobject_id' => 215, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_15', 'sort_field' => 2, 'sort_order' => 1),
485
            array('node_id' => 16, 'main_node_id' => 16, 'parent_node_id' => 15, 'contentobject_id' => 216, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 0, 'priority' => 0, 'path_identification_string' => 'test_16', 'sort_field' => 2, 'sort_order' => 1),
486
        );
487
        $destinationData = array('node_id' => 5, 'main_node_id' => 5, 'parent_node_id' => 4, 'contentobject_id' => 200, 'contentobject_version' => 1, 'is_hidden' => 0, 'is_invisible' => 1, 'path_identification_string' => 'test_destination');
488
        $mainLocationsMap = array(true, true, true, true, 1011, 1012, true);
489
        $updateMainLocationsMap = array(1215 => 1015);
490
        $offset = 1000;
491
492
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
493
            ->expects($this->once())
494
            ->method('getSubtreeContent')
495
            ->with($subtreeContentRows[0]['node_id'])
496
            ->will($this->returnValue($subtreeContentRows));
497
        $this->locationGateway
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ntent\Location\Gateway>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
498
            ->expects($this->once())
499
            ->method('getBasicNodeData')
500
            ->with($destinationData['node_id'])
501
            ->will($this->returnValue($destinationData));
502
503
        $objectStateHandlerCall = 0;
504
        $this->objectStateHandler->expects($this->at($objectStateHandlerCall++))
505
            ->method('loadAllGroups')
506
            ->will(
507
                $this->returnValue(
508
                    array(
509
                        new ObjectStateGroup(array('id' => 10)),
510
                        new ObjectStateGroup(array('id' => 20)),
511
                    )
512
                )
513
            );
514
        $this->objectStateHandler->expects($this->at($objectStateHandlerCall++))
515
            ->method('loadObjectStates')
516
            ->with($this->equalTo(10))
517
            ->will(
518
                $this->returnValue(
519
                    array(
520
                        new ObjectState(array('id' => 11, 'groupId' => 10)),
521
                        new ObjectState(array('id' => 12, 'groupId' => 10)),
522
                    )
523
                )
524
            );
525
        $this->objectStateHandler->expects($this->at($objectStateHandlerCall++))
526
            ->method('loadObjectStates')
527
            ->with($this->equalTo(20))
528
            ->will(
529
                $this->returnValue(
530
                    array(
531
                        new ObjectState(array('id' => 21, 'groupId' => 20)),
532
                        new ObjectState(array('id' => 22, 'groupId' => 20)),
533
                    )
534
                )
535
            );
536
        $defaultObjectStates = array(
537
            new ObjectState(array('id' => 11, 'groupId' => 10)),
538
            new ObjectState(array('id' => 21, 'groupId' => 20)),
539
        );
540
541
        $contentIds = array_values(
542
            array_unique(
543
                array_column($subtreeContentRows, 'contentobject_id')
544
            )
545
        );
546
        foreach ($contentIds as $index => $contentId) {
547
            $this->contentHandler
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...Legacy\Content\Handler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
548
                ->expects($this->at($index * 2))
549
                ->method('copy')
550
                ->with($contentId, 1)
551
                ->will(
552
                    $this->returnValue(
553
                        new Content(
554
                            array(
555
                                'versionInfo' => new VersionInfo(
556
                                    array(
557
                                        'contentInfo' => new ContentInfo(
558
                                            array(
559
                                                'id' => $contentId + $offset,
560
                                                'currentVersionNo' => 1,
561
                                            )
562
                                        ),
563
                                    )
564
                                ),
565
                            )
566
                        )
567
                    )
568
                );
569
570
            foreach ($defaultObjectStates as $objectState) {
571
                $this->objectStateHandler->expects($this->at($objectStateHandlerCall++))
572
                    ->method('setContentState')
573
                    ->with(
574
                        $contentId + $offset,
575
                        $objectState->groupId,
576
                        $objectState->id
577
                    );
578
            }
579
580
            $this->contentHandler
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...Legacy\Content\Handler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
581
                ->expects($this->at($index * 2 + 1))
582
                ->method('publish')
583
                ->with(
584
                    $contentId + $offset,
585
                    1,
586
                    $this->isInstanceOf(Content\MetadataUpdateStruct::class)
587
                )
588
                ->will(
589
                    $this->returnValue(
590
                        new Content(
591
                            array(
592
                                'versionInfo' => new VersionInfo(
593
                                    array(
594
                                        'contentInfo' => new ContentInfo(
595
                                            array(
596
                                                'id' => ($contentId + $offset),
597
                                            )
598
                                        ),
599
                                    )
600
                                ),
601
                            )
602
                        )
603
                    )
604
                );
605
        }
606
        $lastContentHandlerIndex = $index * 2 + 1;
0 ignored issues
show
Bug introduced by
The variable $index seems to be defined by a foreach iteration on line 546. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
607
608
        $pathStrings = array($destinationData['node_id'] => $destinationData['path_identification_string']);
609
        foreach ($subtreeContentRows as $index => $row) {
610
            $mapper = new Mapper();
611
            $createStruct = $mapper->getLocationCreateStruct($row);
612
            $this->locationMapper
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...ontent\Location\Mapper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
613
                ->expects($this->at($index))
614
                ->method('getLocationCreateStruct')
615
                ->with($row)
616
                ->will($this->returnValue($createStruct));
617
618
            $createStruct = clone $createStruct;
619
            $createStruct->contentId = $createStruct->contentId + $offset;
620
            $createStruct->parentId = $index === 0 ? $destinationData['node_id'] : $createStruct->parentId + $offset;
621
            $createStruct->invisible = true;
622
            $createStruct->mainLocationId = $mainLocationsMap[$index];
623
            $createStruct->pathIdentificationString = $pathStrings[$createStruct->parentId] . '/' . $row['path_identification_string'];
624
            $pathStrings[$row['node_id'] + $offset] = $createStruct->pathIdentificationString;
625
            $handler
626
                ->expects($this->at($index))
627
                ->method('create')
628
                ->with($createStruct)
629
                ->will(
630
                    $this->returnValue(
631
                        new Location(
632
                            array(
633
                                'id' => $row['node_id'] + $offset,
634
                                'contentId' => $row['contentobject_id'],
635
                                'hidden' => false,
636
                                'invisible' => true,
637
                                'pathIdentificationString' => $createStruct->pathIdentificationString,
638
                            )
639
                        )
640
                    )
641
                );
642
        }
643
644
        foreach ($updateMainLocationsMap as $contentId => $locationId) {
645
            $handler
646
                ->expects($this->any())
647
                ->method('changeMainLocation')
648
                ->with($contentId, $locationId);
649
        }
650
651
        $handler
652
            ->expects($this->once())
653
            ->method('load')
654
            ->with($destinationData['node_id'])
655
            ->will($this->returnValue(new Location(array('contentId' => $destinationData['contentobject_id']))));
656
657
        $this->contentHandler
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...Legacy\Content\Handler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
658
            ->expects($this->at($lastContentHandlerIndex + 1))
659
            ->method('loadContentInfo')
660
            ->with($destinationData['contentobject_id'])
661
            ->will($this->returnValue(new ContentInfo(array('sectionId' => 12345))));
662
663
        $this->contentHandler
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<eZ\Publish\Core\P...Legacy\Content\Handler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
664
            ->expects($this->at($lastContentHandlerIndex + 2))
665
            ->method('loadContentInfo')
666
            ->with(21)
667
            ->will($this->returnValue(new ContentInfo(array('mainLocationId' => 1010))));
668
669
        $handler
670
            ->expects($this->once())
671
            ->method('setSectionForSubtree')
672
            ->with($subtreeContentRows[0]['node_id'] + $offset, 12345);
673
674
        $handler->copySubtree(
675
            $subtreeContentRows[0]['node_id'],
676
            $destinationData['node_id']
677
        );
678
    }
679
680
    /**
681
     * Returns the handler to test with $methods mocked.
682
     *
683
     * @param string[] $methods
684
     *
685
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler
686
     */
687
    protected function getPartlyMockedHandler(array $methods)
688
    {
689
        return $this->getMockBuilder(LocationHandler::class)
690
            ->setMethods($methods)
691
            ->setConstructorArgs(
692
                array(
693
                    $this->locationGateway = $this->createMock(Gateway::class),
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...ocation\Gateway::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\P...ntent\Location\Gateway> of property $locationGateway.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
694
                    $this->locationMapper = $this->createMock(Mapper::class),
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...Location\Mapper::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\P...ontent\Location\Mapper> of property $locationMapper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
695
                    $this->contentHandler = $this->createMock(ContentHandler::class),
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...Content\Handler::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\P...Legacy\Content\Handler> of property $contentHandler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
696
                    $this->objectStateHandler = $this->createMock(ObjectStateHandler::class),
697
                    $this->treeHandler = $this->createMock(TreeHandler::class),
698
                )
699
            )
700
            ->getMock();
701
    }
702
}
703