1<?php
2
3namespace dokuwiki\plugin\swarmwebhook\test;
4
5/**
6 * General tests for the swarmwebhook plugin
7 *
8 * @group plugin_swarmwebhook
9 * @group plugins
10 */
11class ZapierHandlePayload extends \DokuWikiTest
12{
13    /** @var array alway enable the needed plugins */
14    protected $pluginsEnabled = ['swarmwebhook', 'struct', 'sqlite'];
15
16    function setUp(){
17        global $conf;
18
19        parent::setUp();
20
21        $conf ['plugin']['swarmwebhook']['service'] = 'Zapier';
22    }
23
24    /**
25     * Do not refactor these with a DataProvider -> struct database state would depend on order of execution
26     */
27    public function test_handleWebhookPayload_initial_single()
28    {
29        $inputJSON = '{"createdAt": "1525849118", "like": "False", "isMayor": "True", "editableUntil": "1525935518000", "posts": {"count": "0", "textCount": "0"}, "comments": {"count": "0"}, "photos": {"count": "0", "items": ""}, "likes": {"count": "0", "groups": ""}, "venue": {"stats": {"tipCount": "2", "checkinsCount": "1552", "usersCount": "24"}, "name": "CosmoCode", "venueRatingBlacklisted": "True", "url": "http://www.cosmocode.de", "contact": {"twitter": "cosmocode"}, "location": {"city": "Berlin", "labeledLatLngs": "label: display\nlat: 52.5341728565\nlng: 13.4235969339", "cc": "DE", "country": "Germany", "postalCode": "10405", "state": "Berlin", "formattedAddress": "Prenzlauer Allee 36 (Marienburger Strasse),10405 Berlin", "crossStreet": "Marienburger Strasse", "address": "Prenzlauer Allee 36", "lat": "52.5341728565", "lng": "13.4235969339"}, "beenHere": {"lastCheckinExpiredAt": "0"}, "verified": "False", "id": "4b4ca6c8f964a520f8b826e3", "categories": "icon: {u\'prefix\': u\'https://ss3.4sqi.net/img/categories_v2/building/default_\', u\'suffix\': u\'.png\'}\nid: 4bf58dd8d48988d124941735\nname: Office\npluralName: Offices\nprimary: True\nshortName: Office"}, "type": "checkin", "id": "5af29c1e6fd626002c38730b", "timeZoneOffset": "120", "source": {"url": "https://www.swarmapp.com", "name": "Swarm for Android"}}';
30        $webhook = new mock\Zapier();
31
32        $webhook->run($inputJSON);
33
34        /** @var \remote_plugin_struct $remote */
35        $remote = plugin_load('remote', 'struct');
36        $rows = $remote->getAggregationData(
37            ['swarm'],
38            ['*']
39        );
40
41        $expectedRows = [
42            'swarm.date' => '2018-05-09',
43            'swarm.json' => $inputJSON,
44            'swarm.locname' => 'CosmoCode',
45            'swarm.checkinid' => '5af29c1e6fd626002c38730b',
46            'swarm.shout' => '',
47            'swarm.time' => '2018-05-09 08:58',
48            'swarm.service' => 'Zapier',
49        ];
50
51        $this->assertEquals($rows[0], $expectedRows, 'single event, initially creating the schema');
52    }
53
54    /**
55     * Do not refactor these with a DataProvider -> struct database state would depend on order of execution
56     */
57    public function test_handleWebhookPayload_initial_double()
58    {
59        $inputJSON = '{"createdAt": "1525849118", "like": "False", "isMayor": "True", "editableUntil": "1525935518000", "posts": {"count": "0", "textCount": "0"}, "comments": {"count": "0"}, "photos": {"count": "0", "items": ""}, "likes": {"count": "0", "groups": ""}, "venue": {"stats": {"tipCount": "2", "checkinsCount": "1552", "usersCount": "24"}, "name": "CosmoCode", "venueRatingBlacklisted": "True", "url": "http://www.cosmocode.de", "contact": {"twitter": "cosmocode"}, "location": {"city": "Berlin", "labeledLatLngs": "label: display\nlat: 52.5341728565\nlng: 13.4235969339", "cc": "DE", "country": "Germany", "postalCode": "10405", "state": "Berlin", "formattedAddress": "Prenzlauer Allee 36 (Marienburger Strasse),10405 Berlin", "crossStreet": "Marienburger Strasse", "address": "Prenzlauer Allee 36", "lat": "52.5341728565", "lng": "13.4235969339"}, "beenHere": {"lastCheckinExpiredAt": "0"}, "verified": "False", "id": "4b4ca6c8f964a520f8b826e3", "categories": "icon: {u\'prefix\': u\'https://ss3.4sqi.net/img/categories_v2/building/default_\', u\'suffix\': u\'.png\'}\nid: 4bf58dd8d48988d124941735\nname: Office\npluralName: Offices\nprimary: True\nshortName: Office"}, "type": "checkin", "id": "5af29c1e6fd626002c38730b", "timeZoneOffset": "120", "source": {"url": "https://www.swarmapp.com", "name": "Swarm for Android"}}';
60        $webhook = new mock\Zapier();
61
62        $actualOK = $webhook->handleWebhookPayload($inputJSON);
63        $actualOK = $actualOK && $webhook->handleWebhookPayload($inputJSON);
64
65        /** @var \remote_plugin_struct $remote */
66        $remote = plugin_load('remote', 'struct');
67        $rows = $remote->getAggregationData(
68            ['swarm'],
69            ['*']
70        );
71
72        $expectedRows = [
73            'swarm.date' => '2018-05-09',
74            'swarm.json' => $inputJSON,
75            'swarm.locname' => 'CosmoCode',
76            'swarm.checkinid' => '5af29c1e6fd626002c38730b',
77            'swarm.shout' => '',
78            'swarm.time' => '2018-05-09 08:58',
79            'swarm.service' => 'Zapier',
80        ];
81
82        $this->assertTrue($actualOK, 'single event, initially creating the schema');
83        $this->assertCount(1, $rows, 'saving a payload twice should only create one row');
84        $this->assertEquals($rows[0], $expectedRows);
85    }
86}
87