3
* Spring Signage Ltd - http://www.springsignage.com
4
* Copyright (C) 2015 Spring Signage Ltd
8
namespace Xibo\Tests\Integration;
10
use Xibo\Helper\Random;
11
use Xibo\OAuth2\Client\Entity\XiboDisplay;
12
use Xibo\OAuth2\Client\Entity\XiboLayout;
13
use Xibo\OAuth2\Client\Entity\XiboLibrary;
14
use Xibo\OAuth2\Client\Entity\XiboPlaylist;
15
use Xibo\OAuth2\Client\Entity\XiboRegion;
16
use Xibo\OAuth2\Client\Entity\XiboStats;
17
use Xibo\OAuth2\Client\Entity\XiboText;
18
use Xibo\Tests\LocalWebTestCase;
21
* Class StatisticsTest
22
* @package Xibo\Tests\Integration
24
class StatisticsTest extends LocalWebTestCase
28
protected $startMedias;
29
protected $startLayouts;
30
protected $startDisplays;
33
* setUp - called before every test automatically
35
public function setup()
38
$this->startMedias = (new XiboLibrary($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
39
$this->startLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
40
$this->startDisplays = (new XiboDisplay($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
44
* tearDown - called after every test automatically
46
public function tearDown()
48
// tearDown all media files that weren't there initially
49
$finalMedias = (new XiboLibrary($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
50
# Loop over any remaining media files and nuke them
51
foreach ($finalMedias as $media) {
52
/** @var XiboLibrary $media */
54
foreach ($this->startMedias as $startMedia) {
55
if ($startMedia->mediaId == $media->mediaId) {
61
$media->deleteAssigned();
62
} catch (\Exception $e) {
63
fwrite(STDERR, 'Unable to delete ' . $media->mediaId . '. E:' . $e->getMessage());
68
// tearDown all layouts that weren't there initially
69
$finalLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
70
# Loop over any remaining layouts and nuke them
71
foreach ($finalLayouts as $layout) {
72
/** @var XiboLayout $layout */
74
foreach ($this->startLayouts as $startLayout) {
75
if ($startLayout->layoutId == $layout->layoutId) {
82
} catch (\Exception $e) {
83
fwrite(STDERR, 'Layout: Unable to delete ' . $layout->layoutId . '. E:' . $e->getMessage());
88
// Tear down any displays that weren't there before
89
$finalDisplays = (new XiboDisplay($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
91
# Loop over any remaining displays and nuke them
92
foreach ($finalDisplays as $display) {
93
/** @var XiboDisplay $display */
95
foreach ($this->startDisplays as $startDisplay) {
96
if ($startDisplay->displayId == $display->displayId) {
103
} catch (\Exception $e) {
104
fwrite(STDERR, 'Unable to delete ' . $display->displayId . '. E:' . $e->getMessage());
112
* Test the method call with default values
115
public function testListAll()
117
$this->client->get('/stats');
119
$this->assertSame(200, $this->client->response->status());
120
$this->assertNotEmpty($this->client->response->body());
121
$object = json_decode($this->client->response->body());
122
$this->assertObjectHasAttribute('data', $object, $this->client->response->body());
127
* Check if proof of play statistics are correct
129
public function testProof()
131
# Create a Display in the system
132
$hardwareId = Random::generateString(12, 'phpunit');
133
$response = $this->getXmdsWrapper()->RegisterDisplay($hardwareId, 'PHPUnit Test Display');
134
# Now find the Id of that Display
135
$displays = (new XiboDisplay($this->getEntityProvider()))->get();
138
foreach ($displays as $disp) {
139
if ($disp->license == $hardwareId) {
144
if ($display === null) {
145
$this->fail('Display was not added correctly');
147
# Create layout with random name
148
$name = Random::generateString(8, 'phpunit');
149
$layout = (new XiboLayout($this->getEntityProvider()))->create($name, 'phpunit description', '', 9);
150
# Add two regions to our layout
151
$region = (new XiboRegion($this->getEntityProvider()))->create($layout->layoutId, 200,300,75,125);
152
$region2 = (new XiboRegion($this->getEntityProvider()))->create($layout->layoutId, 100,100,475,425);
153
# Upload three media files
154
$media = (new XiboLibrary($this->getEntityProvider()))->create('API image', PROJECT_ROOT . '/tests/resources/xts-night-001.jpg');
155
$media2 = (new XiboLibrary($this->getEntityProvider()))->create('API image 2', PROJECT_ROOT . '/tests/resources/xts-layout-003-background.jpg');
156
# Create and assign new text widget
157
$text = (new XiboText($this->getEntityProvider()))->create('Text item', 10, 1, 'marqueeRight', 5, null, null, 'TEST API TEXT', null, $region2->playlists[0]['playlistId']);
158
# Assign media to a playlists
159
$playlist = (new XiboPlaylist($this->getEntityProvider()))->assign([$media->mediaId, $media2->mediaId], 10, $region->playlists[0]['playlistId']);
161
$widget = $playlist->widgets[0];
162
$widget2 = $playlist->widgets[1];
163
# Set start and date time
165
$fromDt = '2017-02-12 00:00:00';
166
$toDt = '2017-02-15 00:00:00';
168
$fromDt2 = '2017-02-12 00:00:00';
169
$toDt2 = '2017-02-14 00:00:00';
171
$fromDt3 = '2017-02-14 00:00:00';
172
$toDt3 = '2017-02-15 00:00:00';
174
$fromDt4 = '2017-02-15 00:00:00';
175
$toDt4 = '2017-02-16 00:00:00';
177
# Add stats to the DB - known set
179
# 1 layout, 1 region, 1 media
180
# type,start,end,layout,media
181
# layout,2016-10-12 00:00:00, 2016-10-15 00:00:00, L1, NULL
182
# media,2016-10-12 00:00:00, 2016-10-15 00:00:00, L1, M1
188
# 1 layout, 1 region, 2 medias
189
# type,start,end,layout,media
190
# layout,2016-10-12 00:00:00, 2016-10-15 00:00:00, L1, NULL
191
# media,2016-10-12 00:00:00, 2016-10-13 00:00:00, L1, M1
192
# media,2016-10-13 00:00:00, 2016-10-15 00:00:00, L1, M2
199
# 1 layout, 2 region, 2 medias (1 per region)
200
# type,start,end,layout,media
201
# layout,2016-10-12 00:00:00, 2016-10-15 00:00:00, L1, NULL
202
# media,2016-10-12 00:00:00, 2016-10-13 00:00:00, L1, M1
203
# media,2016-10-13 00:00:00, 2016-10-14 00:00:00, L1, M1
204
# media,2016-10-14 00:00:00, 2016-10-15 00:00:00, L1, M1
205
# media,2016-10-12 00:00:00, 2016-10-15 00:00:00, L1, M2
216
self::$container->store->insert('
217
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
218
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
221
'statDate' => date("Y-m-d H:i:s"),
222
'start' => '2017-02-12 00:00:00',
223
'end' => '2017-02-15 00:00:00',
225
'displayId' => $display->displayId,
226
'layoutId' => $layout->layoutId,
231
self::$container->store->insert('
232
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
233
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
236
'statDate' => date("Y-m-d H:i:s"),
237
'start' => '2017-02-12 00:00:00',
238
'end' => '2017-02-13 00:00:00',
240
'displayId' => $display->displayId,
241
'layoutId' => $layout->layoutId,
242
'mediaId' => $media->mediaId,
244
'widgetId' => $widget->widgetId
246
self::$container->store->insert('
247
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
248
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
251
'statDate' => date("Y-m-d H:i:s"),
252
'start' => '2017-02-14 00:00:00',
253
'end' => '2017-02-15 00:00:00',
255
'displayId' => $display->displayId,
256
'layoutId' => $layout->layoutId,
257
'mediaId' => $media2->mediaId,
259
'widgetId' => $widget2->widgetId
261
self::$container->store->insert('
262
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
263
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
266
'statDate' => date("Y-m-d H:i:s"),
267
'start' => '2017-02-12 00:00:00',
268
'end' => '2017-02-15 00:00:00',
270
'displayId' => $display->displayId,
271
'layoutId' => $layout->layoutId,
274
'widgetId' => $text->widgetId
278
self::$container->store->insert('
279
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
280
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
283
'statDate' => date("Y-m-d H:i:s"),
284
'start' => '2017-02-15 00:00:00',
285
'end' => '2017-02-16 00:00:00',
287
'displayId' => $display->displayId,
288
'layoutId' => $layout->layoutId,
293
self::$container->store->insert('
294
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
295
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
298
'statDate' => date("Y-m-d H:i:s"),
299
'start' => '2017-02-13 00:00:00',
300
'end' => '2017-02-14 00:00:00',
302
'displayId' => $display->displayId,
303
'layoutId' => $layout->layoutId,
304
'mediaId' => $media->mediaId,
306
'widgetId' => $widget->widgetId
308
self::$container->store->insert('
309
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
310
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
313
'statDate' => date("Y-m-d H:i:s"),
314
'start' => '2017-02-15 00:00:00',
315
'end' => '2017-02-16 00:00:00',
317
'displayId' => $display->displayId,
318
'layoutId' => $layout->layoutId,
319
'mediaId' => $media2->mediaId,
321
'widgetId' => $widget2->widgetId
323
self::$container->store->insert('
324
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
325
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
328
'statDate' => date("Y-m-d H:i:s"),
329
'start' => '2017-02-15 00:00:00',
330
'end' => '2017-02-16 00:00:00',
332
'displayId' => $display->displayId,
333
'layoutId' => $layout->layoutId,
336
'widgetId' => $text->widgetId
340
self::$container->store->insert('
341
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
342
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
345
'statDate' => date("Y-m-d H:i:s"),
346
'start' => '2017-02-16 00:00:00',
347
'end' => '2017-02-17 00:00:00',
349
'displayId' => $display->displayId,
350
'layoutId' => $layout->layoutId,
355
self::$container->store->insert('
356
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
357
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
360
'statDate' => date("Y-m-d H:i:s"),
361
'start' => '2017-02-16 12:00:00',
362
'end' => '2017-02-17 00:00:00',
364
'displayId' => $display->displayId,
365
'layoutId' => $layout->layoutId,
366
'mediaId' => $media->mediaId,
368
'widgetId' => $widget->widgetId
370
self::$container->store->insert('
371
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
372
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
375
'statDate' => date("Y-m-d H:i:s"),
376
'start' => '2017-02-16 00:00:00',
377
'end' => '2017-02-16 12:00:00',
379
'displayId' => $display->displayId,
380
'layoutId' => $layout->layoutId,
381
'mediaId' => $media2->mediaId,
383
'widgetId' => $widget2->widgetId
385
self::$container->store->insert('
386
INSERT INTO `stat` (type, statDate, start, end, scheduleID, displayID, layoutID, mediaID, Tag, widgetId)
387
VALUES (:type, :statDate, :start, :end, :scheduleId, :displayId, :layoutId, :mediaId, :tag, :widgetId)
390
'statDate' => date("Y-m-d H:i:s"),
391
'start' => '2017-02-16 00:00:00',
392
'end' => '2017-02-17 00:00:00',
394
'displayId' => $display->displayId,
395
'layoutId' => $layout->layoutId,
398
'widgetId' => $text->widgetId
401
self::$container->store->commitIfNecessary();
402
# get stats and see if they match with what we expect
403
$this->client->get('/stats' , [
404
'fromDt' => '2017-02-12 00:00:00',
405
'toDt' => '2017-02-17 00:00:00',
406
'displayId' => $display->displayId
409
$this->assertSame(200, $this->client->response->status());
410
$this->assertNotEmpty($this->client->response->body());
411
$object = json_decode($this->client->response->body());
412
//fwrite(STDERR, $this->client->response->body());
413
$this->assertObjectHasAttribute('data', $object, $this->client->response->body());
414
$stats = (new XiboStats($this->getEntityProvider()))->get([$layout->layoutId]);
416
self::$container->store->update('DELETE FROM `stat`', []);