~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to tests/integration/DisplayGroupTest.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2015 Spring Signage Ltd
5
 
 * (DisplayGroupTest.php)
6
 
 */
7
 
 
8
 
namespace Xibo\Tests\Integration;
9
 
 
10
 
use Xibo\Helper\Random;
11
 
use Xibo\OAuth2\Client\Entity\XiboCommand;
12
 
use Xibo\OAuth2\Client\Entity\XiboDisplay;
13
 
use Xibo\OAuth2\Client\Entity\XiboDisplayGroup;
14
 
use Xibo\OAuth2\Client\Entity\XiboLayout;
15
 
use Xibo\OAuth2\Client\Entity\XiboLibrary;
16
 
use Xibo\Tests\LocalWebTestCase;
17
 
 
18
 
/**
19
 
 * Class DisplayGroupTest
20
 
 * @package Xibo\Tests\Integration
21
 
 */
22
 
class DisplayGroupTest extends LocalWebTestCase
23
 
{
24
 
    protected $startDisplayGroups;
25
 
    protected $startDisplays;
26
 
 
27
 
    /**
28
 
     * setUp - called before every test automatically
29
 
     */
30
 
    public function setup()
31
 
    {
32
 
        parent::setup();
33
 
        $this->startDisplayGroups = (new XiboDisplayGroup($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
34
 
        $this->startDisplays = (new XiboDisplay($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
35
 
        $this->startLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
36
 
        $this->startCommands = (new XiboCommand($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
37
 
    }
38
 
 
39
 
    /**
40
 
     * tearDown - called after every test automatically
41
 
     */
42
 
    public function tearDown()
43
 
    {
44
 
        // tearDown all display groups that weren't there initially
45
 
        $finalDisplayGroups = (new XiboDisplayGroup($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
46
 
 
47
 
        # Loop over any remaining display groups and nuke them
48
 
        foreach ($finalDisplayGroups as $displayGroup) {
49
 
            /** @var XiboDisplayGroup $displayGroup */
50
 
 
51
 
            $flag = true;
52
 
 
53
 
            foreach ($this->startDisplayGroups as $startGroup) {
54
 
               if ($startGroup->displayGroupId == $displayGroup->displayGroupId) {
55
 
                   $flag = false;
56
 
               }
57
 
            }
58
 
 
59
 
            if ($flag) {
60
 
                try {
61
 
                    $displayGroup->delete();
62
 
                } catch (\Exception $e) {
63
 
                    fwrite(STDERR, 'Unable to delete ' . $displayGroup->displayGroupId . '. E:' . $e->getMessage());
64
 
                }
65
 
            }
66
 
        }
67
 
 
68
 
        // Tear down any displays that weren't there before
69
 
        $finalDisplays = (new XiboDisplay($this->getEntityProvider()))->get();
70
 
        
71
 
        # Loop over any remaining displays and nuke them
72
 
        foreach ($finalDisplays as $display) {
73
 
            /** @var XiboDisplay $display */
74
 
 
75
 
            $flag = true;
76
 
 
77
 
            foreach ($this->startDisplays as $startDisplay) {
78
 
               if ($startDisplay->displayId == $display->displayId) {
79
 
                   $flag = false;
80
 
               }
81
 
            }
82
 
 
83
 
            if ($flag) {
84
 
                try {
85
 
                    $display->delete();
86
 
                } catch (\Exception $e) {
87
 
                    fwrite(STDERR, 'Unable to delete ' . $display->displayId . '. E:' . $e->getMessage());
88
 
                }
89
 
            }
90
 
        }
91
 
 
92
 
        // tearDown all layouts that weren't there initially
93
 
        $finalLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
94
 
        # Loop over any remaining layouts and nuke them
95
 
        foreach ($finalLayouts as $layout) {
96
 
            /** @var XiboLayout $layout */
97
 
            $flag = true;
98
 
            foreach ($this->startLayouts as $startLayout) {
99
 
               if ($startLayout->layoutId == $layout->layoutId) {
100
 
                   $flag = false;
101
 
               }
102
 
            }
103
 
            if ($flag) {
104
 
                try {
105
 
                    $layout->delete();
106
 
                } catch (\Exception $e) {
107
 
                    fwrite(STDERR, 'Unable to delete ' . $layout->layoutId . '. E:' . $e->getMessage());
108
 
                }
109
 
            }
110
 
        }
111
 
 
112
 
        // tearDown all commands that weren't there initially
113
 
        $finalCommands = (new XiboCommand($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
114
 
        # Loop over any remaining commands and nuke them
115
 
        foreach ($finalCommands as $command) {
116
 
            /** @var XiboCommand $command */
117
 
            $flag = true;
118
 
            foreach ($this->startCommands as $startCom) {
119
 
               if ($startCom->commandId == $command->commandId) {
120
 
                   $flag = false;
121
 
               }
122
 
            }
123
 
            if ($flag) {
124
 
                try {
125
 
                    $command->delete();
126
 
                } catch (\Exception $e) {
127
 
                    fwrite(STDERR, 'Unable to delete ' . $command->commandId . '. E:' . $e->getMessage());
128
 
                }
129
 
            }
130
 
        }
131
 
 
132
 
        parent::tearDown();
133
 
    }
134
 
 
135
 
    /**
136
 
     *  List all display groups known empty
137
 
     *  @group minimal
138
 
     *  @group destructive
139
 
     */
140
 
    public function testListEmpty()
141
 
    {
142
 
        if (count($this->startDisplayGroups) > 0) {
143
 
            $this->skipTest("There are pre-existing DisplayGroups");
144
 
            return;
145
 
        }
146
 
        $this->client->get('/displaygroup');
147
 
        $this->assertSame(200, $this->client->response->status());
148
 
        $this->assertNotEmpty($this->client->response->body());
149
 
        $object = json_decode($this->client->response->body());
150
 
        $this->assertObjectHasAttribute('data', $object, $this->client->response->body());
151
 
        # There should be no DisplayGroups in the system
152
 
        $this->assertEquals(0, $object->data->recordsTotal);
153
 
    }
154
 
 
155
 
    /**
156
 
     * testAddSuccess - test adding various Display Groups that should be valid
157
 
     * @dataProvider provideSuccessCases
158
 
     * @group minimal
159
 
     */
160
 
    public function testAddSuccess($groupName, $groupDescription, $isDynamic, $expectedDynamic, $dynamicCriteria, $expectedDynamicCriteria)
161
 
    {
162
 
        // Loop through any pre-existing DisplayGroups to make sure we're not
163
 
        // going to get a clash
164
 
 
165
 
        foreach ($this->startDisplayGroups as $tmpGroup) {
166
 
            if ($tmpGroup->displayGroup == $groupName) {
167
 
                $this->skipTest("There is a pre-existing DisplayGroup with this name");
168
 
                return;
169
 
            }
170
 
        }
171
 
 
172
 
        $response = $this->client->post('/displaygroup', [
173
 
            'displayGroup' => $groupName,
174
 
            'description' => $groupDescription,
175
 
            'isDynamic' => $isDynamic,
176
 
            'dynamicCriteria' => $dynamicCriteria
177
 
        ]);
178
 
 
179
 
        $this->assertSame(200, $this->client->response->status(), "Not successful: " . $response);
180
 
 
181
 
        $object = json_decode($this->client->response->body());
182
 
        $this->assertObjectHasAttribute('data', $object);
183
 
        $this->assertObjectHasAttribute('id', $object);
184
 
        $this->assertSame($groupName, $object->data->displayGroup);
185
 
        $this->assertSame($groupDescription, $object->data->description);
186
 
        $this->assertSame($expectedDynamic, $object->data->isDynamic);
187
 
        $this->assertSame($expectedDynamicCriteria, $object->data->dynamicCriteria);
188
 
        # Check that the group was really added
189
 
        $displayGroups = (new XiboDisplayGroup($this->getEntityProvider()))->get(['length' => 1000]);
190
 
        $this->assertEquals(count($this->startDisplayGroups) + 1, count($displayGroups));
191
 
        # Check that the group was added correctly
192
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->getById($object->id);
193
 
        $this->assertSame($groupName, $displayGroup->displayGroup);
194
 
        $this->assertSame($groupDescription, $displayGroup->description);
195
 
        $this->assertSame($expectedDynamic, $displayGroup->isDynamic);
196
 
        $this->assertSame($expectedDynamicCriteria, $displayGroup->dynamicCriteria);
197
 
        # Clean up the DisplayGroup as we no longer need it
198
 
        $this->assertTrue($displayGroup->delete(), 'Unable to delete ' . $displayGroup->displayGroupId);
199
 
    }
200
 
 
201
 
    /**
202
 
     * testAddFailure - test adding various Display Groups that should be invalid
203
 
     * @dataProvider provideFailureCases
204
 
     * @group minimal
205
 
     */
206
 
    public function testAddFailure($groupName, $groupDescription, $isDynamic, $dynamicCriteria)
207
 
    {
208
 
        $response = $this->client->post('/displaygroup', [
209
 
            'displayGroup' => $groupName,
210
 
            'description' => $groupDescription,
211
 
            'isDynamic' => $isDynamic,
212
 
            'dynamicCriteria' => $dynamicCriteria
213
 
        ]);
214
 
 
215
 
        $this->assertSame(500, $this->client->response->status(), 'Expecting failure, received ' . $this->client->response->status());
216
 
    }
217
 
 
218
 
    /**
219
 
     *  List all display groups known set
220
 
     *  @group minimal
221
 
     *  @depends testAddSuccess
222
 
     */
223
 
    public function testListKnown()
224
 
    {
225
 
        # Load in a known set of display groups
226
 
        # We can assume this works since we depend upon the test which
227
 
        # has previously added and removed these without issue:
228
 
        $cases =  $this->provideSuccessCases();
229
 
        $displayGroups = [];
230
 
        // Check each possible case to ensure it's not pre-existing
231
 
        // If it is, skip over it
232
 
        foreach ($cases as $case) {
233
 
            $flag = true;
234
 
 
235
 
            foreach ($this->startDisplayGroups as $tmpGroup) {
236
 
                if ($case[0] == $tmpGroup->displayGroup) {
237
 
                    $flag = false;
238
 
                }
239
 
            }
240
 
 
241
 
            if ($flag) {
242
 
                $displayGroups[] = (new XiboDisplayGroup($this->getEntityProvider()))->create($case[0],$case[1],$case[2],$case[3]);
243
 
            }
244
 
        }
245
 
 
246
 
        $this->client->get('/displaygroup');
247
 
        $this->assertSame(200, $this->client->response->status());
248
 
        $this->assertNotEmpty($this->client->response->body());
249
 
        $object = json_decode($this->client->response->body());
250
 
        $this->assertObjectHasAttribute('data', $object, $this->client->response->body());
251
 
        # There should be as many groups as we created plus the number we started with in the system
252
 
        $this->assertEquals(count($displayGroups) + count($this->startDisplayGroups), $object->data->recordsTotal);
253
 
        # Clean up the groups we created
254
 
        foreach ($displayGroups as $group) {
255
 
            $group->delete();
256
 
        }
257
 
    }
258
 
 
259
 
    /**
260
 
     * List specific display groups
261
 
     * @group minimal
262
 
     * @group destructive
263
 
     * @depends testListKnown
264
 
     * @depends testAddSuccess
265
 
     * @dataProvider provideSuccessCases
266
 
     */
267
 
    public function testListFilter($groupName, $groupDescription, $isDynamic, $expectedDynamic, $dynamicCriteria, $expectedDynamicCriteria)
268
 
    {
269
 
        if (count($this->startDisplayGroups) > 0) {
270
 
            $this->skipTest("There are pre-existing DisplayGroups");
271
 
            return;
272
 
        }
273
 
        # Load in a known set of display groups
274
 
        # We can assume this works since we depend upon the test which
275
 
        # has previously added and removed these without issue:
276
 
        $cases =  $this->provideSuccessCases();
277
 
        $displayGroups = [];
278
 
        foreach ($cases as $case) {
279
 
            $displayGroups[] = (new XiboDisplayGroup($this->getEntityProvider()))->create($case[0], $case[1], $case[2], $case[3]);
280
 
        }
281
 
        $this->client->get('/displaygroup', [
282
 
                           'displayGroup' => $groupName
283
 
                           ]);
284
 
 
285
 
        $this->assertSame(200, $this->client->response->status());
286
 
        $this->assertNotEmpty($this->client->response->body());
287
 
 
288
 
        $object = json_decode($this->client->response->body());
289
 
        $this->assertObjectHasAttribute('data', $object, $this->client->response->body());
290
 
        # There should be at least one match
291
 
        $this->assertGreaterThanOrEqual(1, $object->data->recordsTotal);
292
 
        $flag = false;
293
 
        # Check that for the records returned, $groupName is in the groups names
294
 
        foreach ($object->data->data as $group) {
295
 
            if (strpos($groupName, $group->displayGroup) == 0) {
296
 
                $flag = true;
297
 
            }
298
 
            else {
299
 
                // The object we got wasn't the exact one we searched for
300
 
                // Make sure all the words we searched for are in the result
301
 
                foreach (array_map('trim',explode(",",$groupName)) as $word) {
302
 
                    assertTrue((strpos($word, $group->displayGroup) !== false), 'Group returned did not match the query string: ' . $group->displayGroup);
303
 
                }
304
 
            }
305
 
        }
306
 
 
307
 
        $this->assertTrue($flag, 'Search term not found');
308
 
 
309
 
        foreach ($displayGroups as $group) {
310
 
            $group->delete();
311
 
        }
312
 
    }
313
 
 
314
 
 
315
 
    /**
316
 
     * Each array is a test run
317
 
     *  Format
318
 
     *  (Group Name, Group Description, isDynamic, Returned isDynamic (0 or 1),
319
 
     *   Criteria for Dynamic group, Returned Criteria for Dynamic group)
320
 
     *  For example, if you set isDynamic to 0 and send criteria, it will come back
321
 
     *  with criteria = null
322
 
     *  These are reused in other tests so please ensure Group Name is unique
323
 
     *  through the dataset
324
 
     * @return array
325
 
     */
326
 
    public function provideSuccessCases()
327
 
    {
328
 
 
329
 
        return [
330
 
            // Multi-language non-dynamic groups
331
 
            'English 1' => ['phpunit test group', 'Api', 0, 0, '', null],
332
 
            'English 2' => ['another phpunit test group', 'Api', 0, 0, '', null],
333
 
            'French 1' => ['Test de Français 1', 'Bienvenue à la suite de tests Xibo', 0, 0, null, null],
334
 
            'German 1' => ['Deutsch Prüfung 1', 'Weiß mit schwarzem Text', 0, 0, null, null],
335
 
            'Simplified Chinese 1' => ['试验组', '测试组描述', 0, 0, null, null],
336
 
            // Multi-language dynamic groups
337
 
            'English Dynamic 1' => ['phpunit test dynamic group', 'Api', 1, 1, 'test', 'test'],
338
 
            'French Dynamic 1' => ['Test de Français 2', 'Bienvenue à la suite de tests Xibo', 1, 1, 'test', 'test'],
339
 
            'German Dynamic 1' => ['Deutsch Prüfung 2', 'Weiß mit schwarzem Text', 1, 1, 'test', 'test'],
340
 
            // Tests for the various allowed values for isDynamic = 1
341
 
            'isDynamic on' => ['phpunit group dynamic is on', 'Api', 'on', 1, 'test', 'test'],
342
 
            'isDynamic true' => ['phpunit group dynamic is true', 'Api', 'true', 1, 'test', 'test'],
343
 
            // Invalid isDynamic flag (the CMS sanitises these for us to false)
344
 
            'isDynamic is 7 null criteria' => ['Invalid isDynamic flag 1', 'Invalid isDynamic flag', 7, 0, null, null],
345
 
            'isDynamic is 7 with criteria' => ['Invalid isDynamic flag 2 ', 'Invalid isDynamic flag', 7, 0, 'criteria', 'criteria'],
346
 
            'isDynamic is invalid null criteria' => ['Invalid isDynamic flag alpha 1', 'Invalid isDynamic flag alpha', 'invalid', 0, null, null],
347
 
            'isDynamic is invalid with criteria' => ['Invalid isDynamic flag alpha 2', 'Invalid isDynamic flag alpha', 'invalid', 0, 'criteria', 'criteria']
348
 
        ];
349
 
    }
350
 
 
351
 
    /**
352
 
     *  Each array is a test run
353
 
     *  Format
354
 
     *  (Group Name, Group Description, isDynamic, Criteria for Dynamic group)
355
 
     * @return array
356
 
     */
357
 
    public function provideFailureCases()
358
 
    {
359
 
 
360
 
        return [
361
 
            // Description is limited to 255 characters
362
 
            'Description over 254 characters' => ['Too long description', Random::generateString(255), 0, null],
363
 
            // If isDynamic = 1 then criteria must be set
364
 
            'No dynamicCriteria on dynamic group' => ['No dynamic criteria', 'No dynamic criteria', 1, null],
365
 
            // Missing group names
366
 
            'Group name empty' => ['', 'Group name is empty', 0, null],
367
 
            'Group name null' => [null, 'Group name is null', 0, null]
368
 
        ];
369
 
    }
370
 
 
371
 
    /**
372
 
     *  Try and add two display groups with the same name
373
 
     *  @group minimal
374
 
     *  @depends testAddSuccess
375
 
     */
376
 
    public function testAddDuplicate()
377
 
    {
378
 
        $flag = true;
379
 
        foreach ($this->startDisplayGroups as $group) {
380
 
            if ($group->displayGroup == 'phpunit displaygroup') {
381
 
                $flag = false;
382
 
            }
383
 
        }
384
 
 
385
 
        # Load in a known display group if it's not there already
386
 
        if ($flag) {
387
 
            $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create('phpunit displaygroup', 'phpunit displaygroup', 0, '');
388
 
        }
389
 
 
390
 
        $response = $this->client->post('/displaygroup', [
391
 
            'displayGroup' => 'phpunit displaygroup',
392
 
            'description' => 'phpunit displaygroup',
393
 
            'isDynamic' => 0,
394
 
            'dynamicCriteria' => ''
395
 
        ]);
396
 
 
397
 
        $this->assertSame(500, $this->client->response->status(), 'Expecting failure, received ' . $this->client->response->status());
398
 
        $displayGroup->delete();
399
 
    }
400
 
 
401
 
   /**
402
 
    * Edit an existing display group
403
 
    * @depends testAddSuccess
404
 
    * @group minimal
405
 
    */
406
 
    public function testEdit()
407
 
    {
408
 
        foreach ($this->startDisplayGroups as $group) {
409
 
            if ($group->displayGroup == 'phpunit displaygroup') {
410
 
                $this->skipTest('displayGroup already exists with that name');
411
 
                return;
412
 
            }
413
 
        }
414
 
        # Load in a known display group
415
 
        /** @var XiboDisplayGroup $displayGroup */
416
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create('phpunit displaygroup', 'phpunit displaygroup', 0, '');
417
 
        # Change the group name and description
418
 
        # Change it to a dynamic group with a fixed criteria
419
 
        $name = Random::generateString(8, 'phpunit');
420
 
        $description = Random::generateString(8, 'description');
421
 
        $criteria = 'test';
422
 
 
423
 
        $this->client->put('/displaygroup/' . $displayGroup->displayGroupId, [
424
 
            'displayGroup' => $name,
425
 
            'description' => $description,
426
 
            'isDynamic' => 1,
427
 
            'dynamicCriteria' => $criteria
428
 
        ], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
429
 
 
430
 
        $this->assertSame(200, $this->client->response->status(), 'Not successful: ' . $this->client->response->body());
431
 
        $object = json_decode($this->client->response->body());
432
 
        # Examine the returned object and check that it's what we expect
433
 
        $this->assertObjectHasAttribute('data', $object);
434
 
        $this->assertObjectHasAttribute('id', $object);
435
 
        $this->assertSame($name, $object->data->displayGroup);
436
 
        $this->assertSame($description, $object->data->description);
437
 
        $this->assertSame(1, $object->data->isDynamic);
438
 
        $this->assertSame($criteria, $object->data->dynamicCriteria);
439
 
        # Check that the group was actually renamed
440
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->getById($object->id);
441
 
        $this->assertSame($name, $displayGroup->displayGroup);
442
 
        $this->assertSame($description, $displayGroup->description);
443
 
        $this->assertSame(1, $displayGroup->isDynamic);
444
 
        $this->assertSame($criteria, $displayGroup->dynamicCriteria);
445
 
        # Clean up the DisplayGroup as we no longer need it
446
 
        $displayGroup->delete();
447
 
    }
448
 
 
449
 
    /**
450
 
     * Test delete
451
 
     * @depends testAddSuccess
452
 
     * @group minimal
453
 
     */
454
 
    public function testDelete()
455
 
    {
456
 
        $name1 = Random::generateString(8, 'phpunit');
457
 
        $name2 = Random::generateString(8, 'phpunit');
458
 
        # Load in a couple of known display groups
459
 
        $displayGroup1 = (new XiboDisplayGroup($this->getEntityProvider()))->create($name1, 'phpunit description', 0, '');
460
 
        $displayGroup2 = (new XiboDisplayGroup($this->getEntityProvider()))->create($name2, 'phpunit description', 0, '');
461
 
        # Delete the one we created last
462
 
        $this->client->delete('/displaygroup/' . $displayGroup2->displayGroupId);
463
 
        # This should return 204 for success
464
 
        $response = json_decode($this->client->response->body());
465
 
        $this->assertSame(204, $response->status, $this->client->response->body());
466
 
        # Check only one remains
467
 
        $groups = (new XiboDisplayGroup($this->getEntityProvider()))->get();
468
 
        $this->assertEquals(count($this->startDisplayGroups) + 1, count($groups));
469
 
 
470
 
        $flag = false;
471
 
        foreach ($groups as $group) {
472
 
            if ($group->displayGroupId == $displayGroup1->displayGroupId) {
473
 
                $flag = true;
474
 
            }
475
 
        }
476
 
 
477
 
        $this->assertTrue($flag, 'DisplayGroup ID ' . $displayGroup1->displayGroupId . ' was not found after deleting a different DisplayGroup');
478
 
        # Clean up
479
 
        $displayGroup1->delete();
480
 
    }
481
 
 
482
 
    /**
483
 
     * Assign new displays Test
484
 
     * @return mixed
485
 
     */
486
 
    public function testAssignDisplay()
487
 
    {
488
 
        # Create a Display in the system
489
 
        $hardwareId = Random::generateString(12, 'phpunit');
490
 
        $response = $this->getXmdsWrapper()->RegisterDisplay($hardwareId, 'PHPUnit Test Display');
491
 
        # Now find the Id of that Display
492
 
        $displays = (new XiboDisplay($this->getEntityProvider()))->get();
493
 
        $display = null;
494
 
        
495
 
        foreach ($displays as $disp) {
496
 
            if ($disp->license == $hardwareId) {
497
 
                $display = $disp;
498
 
            }
499
 
        }
500
 
        
501
 
        if ($display === null) {
502
 
            $this->fail('Display was not added correctly');
503
 
        }
504
 
        # Create a DisplayGroup to add the display to
505
 
        $name = Random::generateString(8, 'phpunit');
506
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
507
 
        # Call assign display to display group
508
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/display/assign', [
509
 
                            'displayId' => [$display->displayId]
510
 
                             ]);
511
 
        $response = json_decode($this->client->response->body());
512
 
        $this->assertSame(204, $response->status, $this->client->response->body());
513
 
        # Get a list of all Displays in the group
514
 
        $displays = (new XiboDisplay($this->getEntityProvider()))->get(['displayGroupId' => $displayGroup->displayGroupId]);
515
 
        # Check that there's only us in that group
516
 
        $this->assertEquals(1, count($displays));
517
 
        $this->assertEquals($display->displayId, $displays[0]->displayId);
518
 
        # Clean up
519
 
        $displayGroup->delete();
520
 
        $display->delete();
521
 
    }
522
 
 
523
 
    /**
524
 
     * Try to assign display to isDisplaySpecific displayGroupId
525
 
     * @return mixed
526
 
     */
527
 
    public function testAssignDisplayFailure()
528
 
    {
529
 
        # Create a Display in the system
530
 
        $hardwareId = Random::generateString(12, 'phpunit');
531
 
        $response = $this->getXmdsWrapper()->RegisterDisplay($hardwareId, 'PHPUnit Test Display');
532
 
        # Now find the Id of that Display
533
 
        $displays = (new XiboDisplay($this->getEntityProvider()))->get();
534
 
        $display = null;
535
 
        
536
 
        foreach ($displays as $disp) {
537
 
            if ($disp->license == $hardwareId) {
538
 
                $display = $disp;
539
 
            }
540
 
        }
541
 
        
542
 
        if ($display === null) {
543
 
            $this->fail('Display was not added correctly');
544
 
        }
545
 
        # Create a DisplayGroup to add the display to
546
 
        $name = Random::generateString(8, 'phpunit');
547
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
548
 
        # Call assign display to display group
549
 
        $this->client->post('/displaygroup/' . $display->displayGroupId . '/display/assign', [
550
 
                            'displayId' => [$display->displayId]
551
 
                             ]);
552
 
        $this->assertSame(500, $this->client->response->status(), 'Expecting failure, received ' . $this->client->response->status());
553
 
    }
554
 
 
555
 
    /**
556
 
     * Unassign displays Test
557
 
     */
558
 
    public function testUnassignDisplay()
559
 
    {
560
 
        # Create a Display in the system
561
 
        $hardwareId = Random::generateString(12, 'phpunit');
562
 
        $response = $this->getXmdsWrapper()->RegisterDisplay($hardwareId, 'PHPUnit Test Display');
563
 
        # Now find the Id of that Display
564
 
        $displays = (new XiboDisplay($this->getEntityProvider()))->get();
565
 
        $display = null;
566
 
        
567
 
        foreach ($displays as $disp) {
568
 
            if ($disp->license == $hardwareId) {
569
 
                $display = $disp;
570
 
            }
571
 
        }
572
 
        
573
 
        if ($display === null) {
574
 
            $this->fail('Display was not added correctly');
575
 
        }
576
 
 
577
 
        # Create display group
578
 
        $name = Random::generateString(8, 'phpunit');
579
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
580
 
        # Assign display to display group
581
 
        $displayGroup->assignDisplay([$display->displayId]);
582
 
        # Unassign display from display group
583
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/display/unassign', [
584
 
        'displayId' => [$display->displayId]
585
 
        ]);
586
 
 
587
 
        $this->assertSame(200, $this->client->response->status());
588
 
        $object = json_decode($this->client->response->body());
589
 
        $this->assertObjectHasAttribute('data', $object);
590
 
        $this->assertObjectHasAttribute('id', $object);
591
 
        # Clean up
592
 
        $displayGroup->delete();
593
 
        $display->delete();
594
 
    }
595
 
 
596
 
    /**
597
 
     * Try to unassign display from isDisplaySpecific displayGroupId
598
 
     */
599
 
    public function testUnassignDisplayFailure()
600
 
    {
601
 
        # Create a Display in the system
602
 
        $hardwareId = Random::generateString(12, 'phpunit');
603
 
        $response = $this->getXmdsWrapper()->RegisterDisplay($hardwareId, 'PHPUnit Test Display');
604
 
        # Now find the Id of that Display
605
 
        $displays = (new XiboDisplay($this->getEntityProvider()))->get();
606
 
        $display = null;
607
 
        
608
 
        foreach ($displays as $disp) {
609
 
            if ($disp->license == $hardwareId) {
610
 
                $display = $disp;
611
 
            }
612
 
        }
613
 
        
614
 
        if ($display === null) {
615
 
            $this->fail('Display was not added correctly');
616
 
        }
617
 
 
618
 
        # Create display group
619
 
        $name = Random::generateString(8, 'phpunit');
620
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
621
 
        # Assign display to display group - should be successful
622
 
        $displayGroup->assignDisplay([$display->displayId]);
623
 
        # Unassign display from isDisplaySpecific display group - should fail
624
 
        $this->client->post('/displaygroup/' . $display->displayGroupId . '/display/unassign', [
625
 
        'displayId' => [$display->displayId]
626
 
        ]);
627
 
        $this->assertSame(500, $this->client->response->status(), 'Expecting failure, received ' . $this->client->response->status());
628
 
    }
629
 
 
630
 
    /**
631
 
     * Assign new display group Test
632
 
     * @return mixed
633
 
     */
634
 
    public function testAssignGroup()
635
 
    {
636
 
        # Generate new random names
637
 
        $name = Random::generateString(8, 'phpunit');
638
 
        $name2 = Random::generateString(8, 'phpunit');
639
 
        # Create new display group
640
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
641
 
        $displayGroup2 = (new XiboDisplayGroup($this->getEntityProvider()))->create($name2, 'phpunit description', 0, '');
642
 
        # Assign second display group to the first one
643
 
                $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/displayGroup/assign', [
644
 
        'displayGroupId' => [$displayGroup2->displayGroupId]
645
 
        ]);
646
 
        # Check if call was successful
647
 
        $this->assertSame(200, $this->client->response->status());
648
 
        $object = json_decode($this->client->response->body());
649
 
        # Clean up
650
 
        $displayGroup->delete();
651
 
        $displayGroup2->delete();
652
 
    }
653
 
 
654
 
    /**
655
 
     * Unassign displays group Test
656
 
     */
657
 
    public function testUnassignGroup()
658
 
    {
659
 
        # Generate new random names
660
 
        $name = Random::generateString(8, 'PARENT');
661
 
        $name2 = Random::generateString(8, 'CHILD');
662
 
        # Create new display groups
663
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
664
 
        $displayGroup2 = (new XiboDisplayGroup($this->getEntityProvider()))->create($name2, 'phpunit description', 0, '');
665
 
                # Assign second display group to the first one
666
 
 
667
 
        $displayGroup->assignDisplayGroup([$displayGroup2->displayGroupId]);
668
 
        # Unassign second display group from the first one
669
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/displayGroup/unassign', [
670
 
                'displayGroupId' => [$displayGroup2->displayGroupId]
671
 
                ]);
672
 
        # Check if call was successful
673
 
        $this->assertSame(200, $this->client->response->status());
674
 
        $object = json_decode($this->client->response->body());
675
 
        # Clean up
676
 
        $displayGroup->delete();
677
 
        $displayGroup2->delete();
678
 
    }
679
 
 
680
 
    /**
681
 
     * Assign new media file to a group Test
682
 
     */
683
 
    public function testAssignMedia()
684
 
    {
685
 
        # Generate new random name
686
 
        $name = Random::generateString(8, 'phpunit');
687
 
        # Create new display group
688
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
689
 
        # Upload a known files
690
 
        $media = (new XiboLibrary($this->getEntityProvider()))->create('API video 12', PROJECT_ROOT . '/tests/resources/HLH264.mp4');
691
 
        $media2 = (new XiboLibrary($this->getEntityProvider()))->create('API image 12', PROJECT_ROOT . '/tests/resources/xts-night-001.jpg');
692
 
        # Assign two files o the display group and unassign one of them
693
 
                $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/media/assign', [
694
 
                'mediaId' => [$media->mediaId, $media2->mediaId],
695
 
                'unassignMediaId' => [$media2->mediaId]
696
 
                ]);
697
 
 
698
 
        $this->assertSame(200, $this->client->response->status());
699
 
        $object = json_decode($this->client->response->body());
700
 
        # Clean up
701
 
        $displayGroup->delete();
702
 
        $media->delete();
703
 
        $media2->delete();
704
 
    }
705
 
 
706
 
    /**
707
 
     * Unassign media files from a group Test
708
 
     */
709
 
    public function testUnassignMedia()
710
 
    {
711
 
        # Generate new random name
712
 
        $name = Random::generateString(8, 'phpunit');
713
 
        # Create new display group
714
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
715
 
        # Upload a known file
716
 
        $media = (new XiboLibrary($this->getEntityProvider()))->create('API image 29', PROJECT_ROOT . '/tests/resources/xts-night-001.jpg');
717
 
        # Assign media to display Group
718
 
        $displayGroup->assignMedia([$media->mediaId]);
719
 
        # Unassign the media from the display group
720
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/media/unassign', [
721
 
                'mediaId' => [$media->mediaId]
722
 
                ]);
723
 
 
724
 
        $this->assertSame(200, $this->client->response->status());
725
 
        $object = json_decode($this->client->response->body());
726
 
        # Clean up
727
 
        $displayGroup->delete();
728
 
        $media->delete();
729
 
    }
730
 
 
731
 
    /**
732
 
     * Assign new layouts to a group Test
733
 
     */
734
 
    public function testAssignLayout()
735
 
    {
736
 
        # Create new random name
737
 
        $name = Random::generateString(8, 'phpunit');
738
 
        # Create new display group
739
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
740
 
        # Create new layouts
741
 
        $layout = (new XiboLayout($this->getEntityProvider()))->create('test layout', 'test description', '', 9);
742
 
        $layout2 = (new XiboLayout($this->getEntityProvider()))->create('test layout 2', 'test description 2', '', 9);
743
 
        # Assign both layouts to display group then unassign the second layout from it
744
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/layout/assign', [
745
 
                'layoutId' => [$layout->layoutId, $layout2->layoutId],
746
 
                'unassignLayoutsId' => [$layout2->layoutId]
747
 
                ]);
748
 
        # Check if call was successful
749
 
        $this->assertSame(200, $this->client->response->status());
750
 
        $object = json_decode($this->client->response->body());
751
 
        # Clean up
752
 
        $displayGroup->delete();
753
 
        $layout->delete();
754
 
        $layout2->delete();
755
 
    }
756
 
 
757
 
    /**
758
 
     * Unassign layouts from a group Test
759
 
     */
760
 
    public function testUnassignLayout()
761
 
    {
762
 
        # Create new random name
763
 
        $name = Random::generateString(8, 'phpunit');
764
 
        # Create new display group
765
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
766
 
        # Create new layout
767
 
        $layout = (new XiboLayout($this->getEntityProvider()))->create('test layout', 'test description', '', 9);
768
 
        # assign layout to display group
769
 
        $displayGroup->assignLayout([$layout->layoutId]);
770
 
                # unassign layout from display group
771
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/layout/unassign', [
772
 
                'layoutId' => [$layout->layoutId]
773
 
                ]);
774
 
 
775
 
        $this->assertSame(200, $this->client->response->status());
776
 
        $object = json_decode($this->client->response->body());
777
 
        # Clean up
778
 
        $displayGroup->delete();
779
 
        $layout->delete();
780
 
    }
781
 
 
782
 
    /**
783
 
     * Assign apk version to a group
784
 
     */
785
 
    public function testVersion()
786
 
    {
787
 
        # Create new random name
788
 
        $name = Random::generateString(8, 'phpunit');
789
 
        # Create new display group
790
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
791
 
        # Upload a known apk file
792
 
        $media = (new XiboLibrary($this->getEntityProvider()))->create('API imagee', PROJECT_ROOT . '/tests/resources/Xibo_for_Android_v1.7_R61.apk');
793
 
        # Asign apk to the display group
794
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/version', [
795
 
                'mediaId' => $media->mediaId
796
 
                ]);
797
 
 
798
 
        $this->assertSame(200, $this->client->response->status());
799
 
        $object = json_decode($this->client->response->body());
800
 
        # Clean up
801
 
        $displayGroup->delete();
802
 
        $media->delete();
803
 
    }
804
 
 
805
 
    /**
806
 
     * Collect now action test
807
 
     */
808
 
    public function testCollectNow()
809
 
    {
810
 
        # Generate random name
811
 
        $name = Random::generateString(8, 'phpunit');
812
 
        # Create new display group
813
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
814
 
        # Call callectNow
815
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/action/collectNow');
816
 
        # Check if call was successful
817
 
        $this->assertSame(200, $this->client->response->status(), $this->client->response->body());
818
 
        $object = json_decode($this->client->response->body());
819
 
        # Clean up
820
 
        $displayGroup->delete();
821
 
    }
822
 
 
823
 
    /**
824
 
     * Change Layout action test
825
 
     */
826
 
    public function testChangeLayout()
827
 
    {
828
 
        # Generate random name
829
 
        $name = Random::generateString(8, 'phpunit');
830
 
        # Create new display group
831
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
832
 
        # Create new layout
833
 
        $layout = (new XiboLayout($this->getEntityProvider()))->create('test layout', 'test description', '', 9);
834
 
        # Call changeLayout
835
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/action/changeLayout', [
836
 
                'layoutId' => $layout->layoutId,
837
 
                'duration' => 900,
838
 
                'downloadRequired' => 1,
839
 
                'changeMode' => 'queue'
840
 
        ]);
841
 
        # Check if successful
842
 
        $this->assertSame(200, $this->client->response->status(), $this->client->response->body());
843
 
        $object = json_decode($this->client->response->body());
844
 
        # Clean up
845
 
        $displayGroup->delete();
846
 
        $layout->delete();
847
 
    }
848
 
 
849
 
    /**
850
 
     * Revert to Schedule action test
851
 
     */
852
 
    public function testRevertToSchedule()
853
 
    {
854
 
        # Generate random name and create new display group
855
 
        $name = Random::generateString(8, 'phpunit');
856
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
857
 
        # Call RevertToSchedule
858
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/action/revertToSchedule');
859
 
        # Check if successful
860
 
        $this->assertSame(200, $this->client->response->status(), $this->client->response->body());
861
 
        $object = json_decode($this->client->response->body());
862
 
        # Clean up
863
 
        $displayGroup->delete();
864
 
    }
865
 
 
866
 
    /**
867
 
     * Send command action test
868
 
     */
869
 
    public function testCommand()
870
 
    {
871
 
        # Generate random name and create new display group
872
 
        $name = Random::generateString(8, 'phpunit');
873
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create($name, 'phpunit description', 0, '');
874
 
        # Create new command
875
 
        $command = (new XiboCommand($this->getEntityProvider()))->create('phpunit command', 'phpunit description', 'phpunitcode');
876
 
        # Send command to display group
877
 
        $this->client->post('/displaygroup/' . $displayGroup->displayGroupId . '/action/command' , [
878
 
                'commandId' => $command->commandId
879
 
                ]);
880
 
        # Check if successful
881
 
        $this->assertSame(200, $this->client->response->status(), $this->client->response->body());
882
 
        $object = json_decode($this->client->response->body());
883
 
        # Clean up
884
 
        $displayGroup->delete();
885
 
        $command->delete();
886
 
    }
887
 
}