~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to tests/integration/ScheduleTest.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
 
 * (ScheduleTest.php)
6
 
 */
7
 
namespace Xibo\Tests\Integration;
8
 
use Xibo\OAuth2\Client\Entity\XiboCampaign;
9
 
use Xibo\OAuth2\Client\Entity\XiboCommand;
10
 
use Xibo\OAuth2\Client\Entity\XiboDisplayGroup;
11
 
use Xibo\OAuth2\Client\Entity\XiboLayout;
12
 
use Xibo\OAuth2\Client\Entity\XiboSchedule;
13
 
use Xibo\Tests\LocalWebTestCase;
14
 
 
15
 
/**
16
 
 * Class ScheduleTest
17
 
 * @package Xibo\Tests\Integration
18
 
 */
19
 
class ScheduleTest extends LocalWebTestCase
20
 
{
21
 
    protected $route = '/schedule';
22
 
    
23
 
    protected $startCommands;
24
 
    protected $startDisplayGroups;
25
 
    protected $startEvents;
26
 
    protected $startLayouts;
27
 
    protected $startCampaigns;
28
 
    
29
 
    /**
30
 
     * setUp - called before every test automatically
31
 
     */
32
 
    public function setup()
33
 
    {
34
 
        parent::setup();
35
 
        $this->startDisplayGroups = (new XiboDisplayGroup($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
36
 
        $this->startLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
37
 
        $this->startCampaigns = (new XiboCampaign($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
38
 
        $this->startCommands = (new XiboCommand($this->getEntityProvider()))->get(['start' => 0, 'length' => 1000]);
39
 
    }
40
 
    
41
 
    /**
42
 
     * tearDown - called after every test automatically
43
 
     */
44
 
    public function tearDown()
45
 
    {
46
 
        // tearDown all display groups that weren't there initially
47
 
        $finalDisplayGroups = (new XiboDisplayGroup($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
48
 
        # Loop over any remaining display groups and nuke them
49
 
        foreach ($finalDisplayGroups as $displayGroup) {
50
 
            /** @var XiboDisplayGroup $displayGroup */
51
 
            $flag = true;
52
 
            foreach ($this->startDisplayGroups as $startGroup) {
53
 
               if ($startGroup->displayGroupId == $displayGroup->displayGroupId) {
54
 
                   $flag = false;
55
 
               }
56
 
            }
57
 
            if ($flag) {
58
 
                try {
59
 
                    $displayGroup->delete();
60
 
                } catch (\Exception $e) {
61
 
                    fwrite(STDERR, 'Unable to delete ' . $displayGroup->displayGroupId . '. E:' . $e->getMessage());
62
 
                }
63
 
            }
64
 
        }
65
 
        // tearDown all layouts that weren't there initially
66
 
        $finalLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
67
 
        # Loop over any remaining layouts and nuke them
68
 
        foreach ($finalLayouts as $layout) {
69
 
            /** @var XiboLayout $layout */
70
 
            $flag = true;
71
 
            foreach ($this->startLayouts as $startLayout) {
72
 
               if ($startLayout->layoutId == $layout->layoutId) {
73
 
                   $flag = false;
74
 
               }
75
 
            }
76
 
            if ($flag) {
77
 
                try {
78
 
                    $layout->delete();
79
 
                } catch (\Exception $e) {
80
 
                    fwrite(STDERR, 'Layout: Unable to delete ' . $layout->layoutId . '. E:' . $e->getMessage());
81
 
                }
82
 
            }
83
 
        }
84
 
        
85
 
        // tearDown all campaigns that weren't there initially
86
 
        $finalCamapigns = (new XiboCampaign($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
87
 
        # Loop over any remaining campaigns and nuke them
88
 
        foreach ($finalCamapigns as $campaign) {
89
 
            /** @var XiboCampaign $campaign */
90
 
            $flag = true;
91
 
            foreach ($this->startCampaigns as $startCampaign) {
92
 
               if ($startCampaign->campaignId == $campaign->campaignId) {
93
 
                   $flag = false;
94
 
               }
95
 
            }
96
 
            if ($flag) {
97
 
                try {
98
 
                    $campaign->delete();
99
 
                } catch (\Exception $e) {
100
 
                    fwrite(STDERR, 'Campaign: Unable to delete ' . $campaign->campaignId . '. E:' . $e->getMessage());
101
 
                }
102
 
            }
103
 
        }
104
 
        // tearDown all commands that weren't there initially
105
 
        $finalCommands = (new XiboCommand($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
106
 
        # Loop over any remaining commands and nuke them
107
 
        foreach ($finalCommands as $command) {
108
 
            /** @var XiboCommand $command */
109
 
            $flag = true;
110
 
            foreach ($this->startCommands as $startCom) {
111
 
               if ($startCom->commandId == $command->commandId) {
112
 
                   $flag = false;
113
 
               }
114
 
            }
115
 
            if ($flag) {
116
 
                try {
117
 
                    $command->delete();
118
 
                } catch (\Exception $e) {
119
 
                    fwrite(STDERR, 'Command: Unable to delete ' . $command->commandId . '. E:' . $e->getMessage());
120
 
                }
121
 
            }
122
 
        }
123
 
        parent::tearDown();
124
 
    }
125
 
    
126
 
    /**
127
 
     * testListAll
128
 
     */
129
 
    public function testListAll()
130
 
    {
131
 
        # list all scheduled events
132
 
        $this->client->get('/schedule/data/events');
133
 
        $this->assertSame(200, $this->client->response->status());
134
 
        $this->assertNotEmpty($this->client->response->body());
135
 
        $object = json_decode($this->client->response->body());
136
 
        $this->assertObjectHasAttribute('result', $object, $this->client->response->body());
137
 
    }
138
 
    
139
 
    /**
140
 
     * @group add
141
 
     * @dataProvider provideSuccessCasesCampaign
142
 
     */
143
 
    public function testAddEventCampaign($isCampaign, $scheduleFrom, $scheduleTo, $scheduledayPartId, $scheduleRecurrenceType, $scheduleRecurrenceDetail, $scheduleRecurrenceRange, $scheduleOrder, $scheduleIsPriority)
144
 
    {
145
 
        # Create new display group
146
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create('phpunit group', 'phpunit description', 0, '');
147
 
        $layout = null;
148
 
        $campaign = null;
149
 
        # isCampaign checks if we want to add campaign or layout to our event
150
 
        if ($isCampaign) {
151
 
            # Create Campaign
152
 
            /* @var XiboCampaign $campaign */
153
 
            $campaign = (new XiboCampaign($this->getEntityProvider()))->create('phpunit');
154
 
            # Create new event with data from provideSuccessCasesCampaign where isCampaign is set to true
155
 
            $response = $this->client->post($this->route, [
156
 
                'fromDt' => date('Y-m-d H:i:s', $scheduleFrom),
157
 
                'toDt' => date('Y-m-d H:i:s', $scheduleTo),
158
 
                'eventTypeId' => 1,
159
 
                'campaignId' => $campaign->campaignId,
160
 
                'displayGroupIds' => [$displayGroup->displayGroupId],
161
 
                'displayOrder' => $scheduleOrder,
162
 
                'isPriority' => $scheduleIsPriority,
163
 
                'scheduleRecurrenceType' => $scheduleRecurrenceType,
164
 
                'scheduleRecurrenceDetail' => $scheduleRecurrenceDetail,
165
 
                'scheduleRecurrenceRange' => $scheduleRecurrenceRange
166
 
            ]);
167
 
        } else {
168
 
            # Create layout
169
 
            $layout = (new XiboLayout($this->getEntityProvider()))->create('phpunit layout', 'phpunit layout', '', 9);
170
 
            # Create new event with data from provideSuccessCasesCampaign where isCampaign is set to false
171
 
            $response = $this->client->post($this->route, [
172
 
                'fromDt' => date('Y-m-d H:i:s', $scheduleFrom),
173
 
                'toDt' => date('Y-m-d H:i:s', $scheduleTo),
174
 
                'eventTypeId' => 1,
175
 
                'campaignId' => $layout->campaignId,
176
 
                'displayGroupIds' => [$displayGroup->displayGroupId],
177
 
                'displayOrder' => $scheduleOrder,
178
 
                'isPriority' => $scheduleIsPriority,
179
 
                'scheduleRecurrenceType' => $scheduleRecurrenceType,
180
 
                'scheduleRecurrenceDetail' => $scheduleRecurrenceDetail,
181
 
                'scheduleRecurrenceRange' => $scheduleRecurrenceRange
182
 
            ]);
183
 
        }
184
 
        # Check if call was successful
185
 
        $this->assertSame(200, $this->client->response->status(), "Not successful: " . $response);
186
 
        $object = json_decode($this->client->response->body());
187
 
        $this->assertObjectHasAttribute('data', $object);
188
 
        $this->assertObjectHasAttribute('id', $object);
189
 
        # Clean up
190
 
        $displayGroup->delete();
191
 
        if ($campaign != null)
192
 
            $campaign->delete();
193
 
        if ($layout != null)
194
 
            $layout->delete();
195
 
    }
196
 
    
197
 
    /**
198
 
     * Each array is a test run
199
 
     * Format ($isCampaign, $scheduleFrom, $scheduleTo, $scheduledayPartId, $scheduleRecurrenceType, $scheduleRecurrenceDetail, $scheduleRecurrenceRange, $scheduleOrder, $scheduleIsPriority)
200
 
     * @return array
201
 
     */
202
 
    public function provideSuccessCasesCampaign()
203
 
    {
204
 
        # Sets of data used in testAddEventCampaign, first argument (isCampaign) controls if it's layout or campaign
205
 
        return [
206
 
            'Campaign no priority, no recurrence' => [true, time()+3600, time()+7200, 0, NULL, NULL, NULL, 0, 0],
207
 
            'Layout no priority, no recurrence' => [false, time()+3600, time()+7200, 0, NULL, NULL, NULL, 0, 0]
208
 
        ];
209
 
    }
210
 
    
211
 
    /**
212
 
     * @group add
213
 
     * @dataProvider provideSuccessCasesCommand
214
 
     */
215
 
    public function testAddEventCommand($scheduleFrom, $scheduledayPartId, $scheduleRecurrenceType, $scheduleRecurrenceDetail, $scheduleRecurrenceRange, $scheduleOrder, $scheduleIsPriority)
216
 
    {
217
 
        # Create command
218
 
        $command = (new XiboCommand($this->getEntityProvider()))->create('phpunit command', 'phpunit command desc', 'code');
219
 
        # Create Display Group
220
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create('phpunit group', 'phpunit description', 0, '');
221
 
        # Create new event with scheduled command and data from provideSuccessCasesCommand
222
 
            $response = $this->client->post($this->route, [
223
 
                'fromDt' => date('Y-m-d H:i:s', $scheduleFrom),
224
 
                'eventTypeId' => 2,
225
 
                'commandId' => $command->commandId,
226
 
                'displayGroupIds' => [$displayGroup->displayGroupId],
227
 
                'displayOrder' => $scheduleOrder,
228
 
                'isPriority' => $scheduleIsPriority,
229
 
                'scheduleRecurrenceType' => $scheduleRecurrenceType,
230
 
                'scheduleRecurrenceDetail' => $scheduleRecurrenceDetail,
231
 
                'scheduleRecurrenceRange' => $scheduleRecurrenceRange
232
 
            ]);
233
 
        # Check if successful
234
 
        $this->assertSame(200, $this->client->response->status(), "Not successful: " . $response);
235
 
        $object = json_decode($this->client->response->body());
236
 
        $this->assertObjectHasAttribute('data', $object);
237
 
        $this->assertObjectHasAttribute('id', $object);
238
 
        # Clean up
239
 
        $displayGroup->delete();
240
 
        $command->delete();
241
 
    }
242
 
    
243
 
    /**
244
 
     * Each array is a test run
245
 
     * Format ($scheduleFrom, $scheduleDisplays, $scheduledayPartId, $scheduleRecurrenceType, $scheduleRecurrenceDetail, $scheduleRecurrenceRange, $scheduleOrder, $scheduleIsPriority)
246
 
     * @return array
247
 
     */
248
 
    public function provideSuccessCasesCommand()
249
 
    {
250
 
        return [
251
 
            'Command no priority, no recurrence' => [time()+3600, 0, NULL, NULL, NULL, 0, 0],
252
 
        ];
253
 
    }
254
 
 
255
 
        /**
256
 
     * @group add
257
 
     * @dataProvider provideSuccessCasesOverlay
258
 
     */
259
 
    public function testAddEventOverlay($scheduleFrom, $scheduleTo, $scheduleCampaignId, $scheduleDisplays, $scheduledayPartId, $scheduleRecurrenceType, $scheduleRecurrenceDetail, $scheduleRecurrenceRange, $scheduleOrder, $scheduleIsPriority)
260
 
    {
261
 
        # Create new dispay group
262
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create('phpunit group', 'phpunit description', 0, '');
263
 
        # Create layout
264
 
        $layout = (new XiboLayout($this->getEntityProvider()))->create('phpunit layout', 'phpunit layout', '', 9);
265
 
        # Create new event with data from provideSuccessCasesOverlay
266
 
            $response = $this->client->post($this->route, [
267
 
                'fromDt' => date('Y-m-d H:i:s', $scheduleFrom),
268
 
                'toDt' => date('Y-m-d H:i:s', $scheduleTo),
269
 
                'eventTypeId' => 3,
270
 
                'campaignId' => $layout->campaignId,
271
 
                'displayGroupIds' => [$displayGroup->displayGroupId],
272
 
                'displayOrder' => $scheduleOrder,
273
 
                'isPriority' => $scheduleIsPriority,
274
 
                'scheduleRecurrenceType' => $scheduleRecurrenceType,
275
 
                'scheduleRecurrenceDetail' => $scheduleRecurrenceDetail,
276
 
                'scheduleRecurrenceRange' => $scheduleRecurrenceRange
277
 
            ]);
278
 
        # Check if call was successful
279
 
        $this->assertSame(200, $this->client->response->status(), "Not successful: " . $response);
280
 
        $object = json_decode($this->client->response->body());
281
 
        $this->assertObjectHasAttribute('data', $object);
282
 
        $this->assertObjectHasAttribute('id', $object);
283
 
        # Clean up
284
 
        $displayGroup->delete();
285
 
        if ($layout != null)
286
 
            $layout->delete();
287
 
    }
288
 
    
289
 
    /**
290
 
     * Each array is a test run
291
 
     * Format ($scheduleFrom, $scheduleTo, $scheduledayPartId, $scheduleRecurrenceType, $scheduleRecurrenceDetail, $scheduleRecurrenceRange, $scheduleOrder, $scheduleIsPriority)
292
 
     * @return array
293
 
     */
294
 
    public function provideSuccessCasesOverlay()
295
 
    {
296
 
        return [
297
 
             'Overlay, no recurrence' => [time()+3600, time()+7200, 0, NULL, NULL, NULL, 0, 0, 0, 0],
298
 
        ];
299
 
    }
300
 
    /**
301
 
     * @group minimal
302
 
     */
303
 
    public function testEdit()
304
 
    {
305
 
        // Get a Display Group Id
306
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create('phpunit group', 'phpunit description', 0, '');
307
 
        // Create Campaign
308
 
        /* @var XiboCampaign $campaign */
309
 
        $campaign = (new XiboCampaign($this->getEntityProvider()))->create('phpunit');
310
 
        # Create new event
311
 
        $event = (new XiboSchedule($this->getEntityProvider()))->createEventLayout(
312
 
            date('Y-m-d H:i:s', time()+3600),
313
 
            date('Y-m-d H:i:s', time()+7200),
314
 
            $campaign->campaignId,
315
 
            [$displayGroup->displayGroupId],
316
 
            0,
317
 
            NULL,
318
 
            NULL,
319
 
            NULL,
320
 
            0,
321
 
            0
322
 
        );
323
 
        $fromDt = time() + 3600;
324
 
        $toDt = time() + 86400;
325
 
        # Edit event
326
 
        $this->client->put($this->route . '/' . $event->eventId, [
327
 
            'fromDt' => date('Y-m-d H:i:s', $fromDt),
328
 
            'toDt' => date('Y-m-d H:i:s', $toDt),
329
 
            'eventTypeId' => 1,
330
 
            'campaignId' => $event->campaignId,
331
 
            'displayGroupIds' => [$displayGroup->displayGroupId],
332
 
            'displayOrder' => 1,
333
 
            'isPriority' => 1
334
 
        ], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
335
 
        $this->assertSame(200, $this->client->response->status(), "Not successful: " . $this->client->response->body());
336
 
        $object = json_decode($this->client->response->body());
337
 
        $this->assertObjectHasAttribute('data', $object);
338
 
        $this->assertObjectHasAttribute('id', $object);
339
 
        # Check if edit was successful
340
 
        $this->assertSame($toDt, intval($object->data->toDt));
341
 
        $this->assertSame($fromDt, intval($object->data->fromDt));
342
 
        # Tidy up
343
 
        $displayGroup->delete();
344
 
        $campaign->delete();
345
 
    }
346
 
    
347
 
    /**
348
 
     * @param $eventId
349
 
     */
350
 
    public function testDelete()
351
 
    {
352
 
 
353
 
        # Get a Display Group Id
354
 
        $displayGroup = (new XiboDisplayGroup($this->getEntityProvider()))->create('phpunit group', 'phpunit description', 0, '');
355
 
        # Create Campaign
356
 
        /* @var XiboCampaign $campaign */
357
 
        $campaign = (new XiboCampaign($this->getEntityProvider()))->create('phpunit');
358
 
        # Create event
359
 
        $event = (new XiboSchedule($this->getEntityProvider()))->createEventLayout(
360
 
            date('Y-m-d H:i:s', time()+3600),
361
 
            date('Y-m-d H:i:s', time()+7200),
362
 
            $campaign->campaignId,
363
 
            [$displayGroup->displayGroupId],
364
 
            0,
365
 
            NULL,
366
 
            NULL,
367
 
            NULL,
368
 
            0,
369
 
            0
370
 
        );
371
 
        # Delete event
372
 
        $this->client->delete($this->route . '/' . $event->eventId);
373
 
        $this->assertSame(200, $this->client->response->status(), $this->client->response->body());
374
 
        # Clean up
375
 
        $displayGroup->delete();
376
 
        $campaign->delete();
377
 
    }
378
 
}