~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to tests/integration/StatisticsTest.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

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
 
 * (StatisticsTest.php)
6
 
 */
7
 
 
8
 
namespace Xibo\Tests\Integration;
9
 
 
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;
19
 
 
20
 
/**
21
 
 * Class StatisticsTest
22
 
 * @package Xibo\Tests\Integration
23
 
 */
24
 
class StatisticsTest extends LocalWebTestCase
25
 
{
26
 
 
27
 
 
28
 
    protected $startMedias;
29
 
    protected $startLayouts;
30
 
    protected $startDisplays;
31
 
    
32
 
    /**
33
 
     * setUp - called before every test automatically
34
 
     */
35
 
    public function setup()
36
 
    {
37
 
        parent::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]);
41
 
    }
42
 
    
43
 
    /**
44
 
     * tearDown - called after every test automatically
45
 
     */
46
 
    public function tearDown()
47
 
    {
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 */
53
 
            $flag = true;
54
 
            foreach ($this->startMedias as $startMedia) {
55
 
               if ($startMedia->mediaId == $media->mediaId) {
56
 
                   $flag = false;
57
 
               }
58
 
            }
59
 
            if ($flag) {
60
 
                try {
61
 
                    $media->deleteAssigned();
62
 
                } catch (\Exception $e) {
63
 
                    fwrite(STDERR, 'Unable to delete ' . $media->mediaId . '. E:' . $e->getMessage());
64
 
                }
65
 
            }
66
 
        }
67
 
 
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 */
73
 
            $flag = true;
74
 
            foreach ($this->startLayouts as $startLayout) {
75
 
               if ($startLayout->layoutId == $layout->layoutId) {
76
 
                   $flag = false;
77
 
               }
78
 
            }
79
 
            if ($flag) {
80
 
                try {
81
 
                    $layout->delete();
82
 
                } catch (\Exception $e) {
83
 
                    fwrite(STDERR, 'Layout: Unable to delete ' . $layout->layoutId . '. E:' . $e->getMessage());
84
 
                }
85
 
            }
86
 
        }
87
 
 
88
 
        // Tear down any displays that weren't there before
89
 
        $finalDisplays = (new XiboDisplay($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
90
 
        
91
 
        # Loop over any remaining displays and nuke them
92
 
        foreach ($finalDisplays as $display) {
93
 
            /** @var XiboDisplay $display */
94
 
            $flag = true;
95
 
            foreach ($this->startDisplays as $startDisplay) {
96
 
               if ($startDisplay->displayId == $display->displayId) {
97
 
                   $flag = false;
98
 
               }
99
 
            }
100
 
            if ($flag) {
101
 
                try {
102
 
                    $display->delete();
103
 
                } catch (\Exception $e) {
104
 
                    fwrite(STDERR, 'Unable to delete ' . $display->displayId . '. E:' . $e->getMessage());
105
 
                }
106
 
            }
107
 
        }
108
 
        parent::tearDown();
109
 
    }
110
 
 
111
 
    /**
112
 
     * Test the method call with default values
113
 
     * @group broken
114
 
     */
115
 
    public function testListAll()
116
 
    {
117
 
        $this->client->get('/stats');
118
 
 
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());
123
 
    }
124
 
 
125
 
 
126
 
    /**
127
 
     * Check if proof of play statistics are correct
128
 
     */
129
 
    public function testProof()
130
 
    {
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();
136
 
        $display = null;
137
 
        
138
 
        foreach ($displays as $disp) {
139
 
            if ($disp->license == $hardwareId) {
140
 
                $display = $disp;
141
 
            }
142
 
        }
143
 
        
144
 
        if ($display === null) {
145
 
            $this->fail('Display was not added correctly');
146
 
        }
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']);
160
 
        # Get Widget Id
161
 
        $widget = $playlist->widgets[0];
162
 
        $widget2 = $playlist->widgets[1];
163
 
        # Set start and date time
164
 
        /*
165
 
        $fromDt =  '2017-02-12 00:00:00';
166
 
        $toDt =  '2017-02-15 00:00:00';
167
 
 
168
 
        $fromDt2 =  '2017-02-12 00:00:00';
169
 
        $toDt2 =  '2017-02-14 00:00:00';
170
 
 
171
 
        $fromDt3 =  '2017-02-14 00:00:00';
172
 
        $toDt3 =  '2017-02-15 00:00:00';
173
 
         
174
 
        $fromDt4 =  '2017-02-15 00:00:00';
175
 
        $toDt4 =  '2017-02-16 00:00:00';
176
 
        */
177
 
        # Add stats to the DB -  known set
178
 
        # 
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
183
 
        #
184
 
        # Result
185
 
        # L1 72 hours
186
 
        # M1 72 hours
187
 
        #
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
193
 
        #
194
 
        # Result
195
 
        # L1 72 hours
196
 
        # M1 24 hours
197
 
        # M2 48 hours
198
 
        #
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
206
 
        #
207
 
        # Result
208
 
        # L1 72 hours
209
 
        # M1 72 hours
210
 
        # M2 72 hours
211
 
        #
212
 
        #
213
 
 
214
 
 
215
 
        # First insert
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)
219
 
        ', [
220
 
            'type' => 'layout', 
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',
224
 
            'scheduleId' => 0,
225
 
            'displayId' => $display->displayId,
226
 
            'layoutId' => $layout->layoutId,
227
 
            'mediaId' => null,
228
 
            'tag' => null,
229
 
            'widgetId' => null
230
 
        ]);
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)
234
 
        ', [
235
 
            'type' => 'media', 
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',
239
 
            'scheduleId' => 0,
240
 
            'displayId' => $display->displayId,
241
 
            'layoutId' => $layout->layoutId,
242
 
            'mediaId' => $media->mediaId,
243
 
            'tag' => null,
244
 
            'widgetId' => $widget->widgetId
245
 
        ]);
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)
249
 
        ', [
250
 
            'type' => 'media', 
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',
254
 
            'scheduleId' => 0,
255
 
            'displayId' => $display->displayId,
256
 
            'layoutId' => $layout->layoutId,
257
 
            'mediaId' => $media2->mediaId,
258
 
            'tag' => null,
259
 
            'widgetId' => $widget2->widgetId
260
 
        ]);
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)
264
 
        ', [
265
 
            'type' => 'widget',
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',
269
 
            'scheduleId' => 0,
270
 
            'displayId' => $display->displayId,
271
 
            'layoutId' => $layout->layoutId,
272
 
            'mediaId' => null,
273
 
            'tag' => null,
274
 
            'widgetId' => $text->widgetId
275
 
        ]);
276
 
 
277
 
        # Second insert
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)
281
 
        ', [
282
 
            'type' => 'layout', 
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',
286
 
            'scheduleId' => 0,
287
 
            'displayId' => $display->displayId,
288
 
            'layoutId' => $layout->layoutId,
289
 
            'mediaId' => null,
290
 
            'tag' => null,
291
 
            'widgetId' => null
292
 
        ]);
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)
296
 
        ', [
297
 
            'type' => 'media', 
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',
301
 
            'scheduleId' => 0,
302
 
            'displayId' => $display->displayId,
303
 
            'layoutId' => $layout->layoutId,
304
 
            'mediaId' => $media->mediaId,
305
 
            'tag' => null,
306
 
            'widgetId' => $widget->widgetId
307
 
        ]);
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)
311
 
        ', [
312
 
            'type' => 'media', 
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',
316
 
            'scheduleId' => 0,
317
 
            'displayId' => $display->displayId,
318
 
            'layoutId' => $layout->layoutId,
319
 
            'mediaId' => $media2->mediaId,
320
 
            'tag' => null,
321
 
            'widgetId' => $widget2->widgetId
322
 
        ]);
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)
326
 
        ', [
327
 
            'type' => 'widget',
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',
331
 
            'scheduleId' => 0,
332
 
            'displayId' => $display->displayId,
333
 
            'layoutId' => $layout->layoutId,
334
 
            'mediaId' => null,
335
 
            'tag' => null,
336
 
            'widgetId' => $text->widgetId
337
 
        ]);
338
 
        
339
 
        # Third insert
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)
343
 
        ', [
344
 
            'type' => 'layout', 
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',
348
 
            'scheduleId' => 0,
349
 
            'displayId' => $display->displayId,
350
 
            'layoutId' => $layout->layoutId,
351
 
            'mediaId' => null,
352
 
            'tag' => null,
353
 
            'widgetId' => null
354
 
        ]);
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)
358
 
        ', [
359
 
            'type' => 'media', 
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',
363
 
            'scheduleId' => 0,
364
 
            'displayId' => $display->displayId,
365
 
            'layoutId' => $layout->layoutId,
366
 
            'mediaId' => $media->mediaId,
367
 
            'tag' => null,
368
 
            'widgetId' => $widget->widgetId
369
 
        ]);
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)
373
 
        ', [
374
 
            'type' => 'media', 
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',
378
 
            'scheduleId' => 0,
379
 
            'displayId' => $display->displayId,
380
 
            'layoutId' => $layout->layoutId,
381
 
            'mediaId' => $media2->mediaId,
382
 
            'tag' => null,
383
 
            'widgetId' => $widget2->widgetId
384
 
        ]);
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)
388
 
        ', [
389
 
            'type' => 'widget',
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',
393
 
            'scheduleId' => 0,
394
 
            'displayId' => $display->displayId,
395
 
            'layoutId' => $layout->layoutId,
396
 
            'mediaId' => null,
397
 
            'tag' => null,
398
 
            'widgetId' => $text->widgetId
399
 
        ]);        
400
 
 
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
407
 
            ]);
408
 
 
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]);
415
 
        //print_r($stats);
416
 
        self::$container->store->update('DELETE FROM `stat`', []);
417
 
    }
418
 
}