1<?php
2
3/**
4 * Helper tests for the acknowledge plugin
5 *
6 * @group plugin_acknowledge
7 * @group plugins
8 */
9class helper_plugin_acknowledge_test extends DokuWikiTest
10{
11    /** @var array */
12    protected $pluginsEnabled = ['acknowledge', 'sqlite'];
13    /** @var \helper_plugin_acknowledge $helper */
14    protected $helper;
15    /** @var helper_plugin_sqlite */
16    protected $db;
17
18    public static function setUpBeforeClass(): void
19    {
20        parent::setUpBeforeClass();
21        /** @var auth_plugin_authplain $auth */
22        global $auth;
23        $auth->createUser('max', 'none', 'max', 'max@example.com', ['super']);
24    }
25
26    public function setUp(): void
27    {
28        parent::setUp();
29        $this->helper = plugin_load('helper', 'acknowledge');
30
31        $this->db = $this->helper->getDB();
32
33        $pages = "REPLACE INTO pages(page,lastmod)
34            VALUES ('dokuwiki:acktest1', 1560805365),
35            ('dokuwiki:acktest2', 1560805365),
36            ('dokuwiki:acktest3', 1560805365)";
37        $this->db->query($pages);
38
39        $assignments = "REPLACE INTO assignments(page,assignee)
40            VALUES ('dokuwiki:acktest1', 'regular, @super'),
41            ('dokuwiki:acktest2', '@super'),
42            ('dokuwiki:acktest3', '@user')";
43        $this->db->query($assignments);
44
45        $acks = "REPLACE INTO acks(page,user,ack)
46            VALUES ('dokuwiki:acktest3', 'regular', 1550801270),
47            ('dokuwiki:acktest3', 'regular', 1560805555),
48            ('dokuwiki:acktest1', 'max', 1550805770),
49            ('dokuwiki:acktest1', 'max', 1560805770),
50            ('dokuwiki:acktest3', 'max', 1560805000)
51            ";
52        $this->db->query($acks);
53    }
54
55    /**
56     * test latest acknowledgements
57     */
58    public function test_getLatestAcknowledgements()
59    {
60        $actual = $this->helper->getAcknowledgements();
61        $expected = [
62            [
63                'page' => 'dokuwiki:acktest1',
64                'user' => 'max',
65                'ack' => '1560805770',
66                'lastmod' => '1560805365',
67            ],
68            [
69                'page' => 'dokuwiki:acktest3',
70                'user' => 'regular',
71                'ack' => '1560805555',
72                'lastmod' => '1560805365',
73            ],
74            [
75                'page' => 'dokuwiki:acktest3',
76                'user' => 'max',
77                'ack' => '1560805000',
78                'lastmod' => '1560805365',
79            ],
80        ];
81        $this->assertEquals($expected, $actual);
82    }
83
84    /**
85     * test latest acknowledgements limited to 1
86     */
87    public function test_getLimitedAcknowledgements()
88    {
89        $actual = $this->helper->getAcknowledgements(1);
90        $expected = [
91            [
92                'page' => 'dokuwiki:acktest1',
93                'user' => 'max',
94                'ack' => '1560805770',
95                'lastmod' => '1560805365',
96            ],
97        ];
98        $this->assertEquals($expected, $actual);
99    }
100
101    /**
102     * test assignment query
103     */
104    public function test_getUserAssignments()
105    {
106        $actual = $this->helper->getUserAssignments('regular', ['user']);
107        $expected = [
108            [
109                'page' => 'dokuwiki:acktest1',
110                'assignee' => 'regular, @super',
111                'lastmod' => '1560805365',
112                'user' => null,
113                'ack' => null,
114            ],
115        ];
116        $this->assertEquals($expected, $actual);
117
118        $actual = $this->helper->getUserAssignments('max', ['user', 'super']);
119        $expected = [
120            [
121                'page' => 'dokuwiki:acktest2',
122                'assignee' => '@super',
123                'lastmod' => '1560805365',
124                'user' => null,
125                'ack' => null,
126            ],
127            [
128                'page' => 'dokuwiki:acktest3',
129                'assignee' => '@user',
130                'lastmod' => '1560805365',
131                'user' => null,
132                'ack' => null,
133            ],
134        ];
135        $this->assertEquals($expected, $actual);
136    }
137
138    public function test_getUserAcknowledgements()
139    {
140        $actual = $this->helper->getUserAcknowledgements('max', ['user', 'super']);
141        $expected = [
142            [
143                'page' => 'dokuwiki:acktest1',
144                'assignee' => 'regular, @super',
145                'lastmod' => '1560805365',
146                'user' => 'max',
147                'ack' => '1560805770',
148            ],
149            [
150                'page' => 'dokuwiki:acktest2',
151                'assignee' => '@super',
152                'lastmod' => '1560805365',
153                'user' => null,
154                'ack' => null,
155            ],
156            [
157                'page' => 'dokuwiki:acktest3',
158                'assignee' => '@user',
159                'lastmod' => '1560805365',
160                'user' => 'max',
161                'ack' => '1560805000',
162            ],
163        ];
164        $this->assertEquals($expected, $actual);
165    }
166
167    /**
168     * Check what users are assigned to a page that has a user and a group in the database
169     */
170    public function test_getPageAssignees()
171    {
172        $actual = $this->helper->getPageAssignees('dokuwiki:acktest1');
173        $expected = ['regular', 'max'];
174        $this->assertEquals($expected, $actual);
175    }
176
177    /**
178     * Check what acknowledgments are there for a page
179     */
180    public function test_getPageAcknowledgements()
181    {
182        $actual = $this->helper->getPageAcknowledgements('dokuwiki:acktest1');
183        $expected = [
184            [
185                'page' => 'dokuwiki:acktest1',
186                'lastmod' => '1560805365',
187                'user' => 'max',
188                'ack' => '1560805770',
189            ],
190            [
191                'page' => 'dokuwiki:acktest1',
192                'lastmod' => '1560805365',
193                'user' => 'regular',
194                'ack' => null,
195            ],
196
197        ];
198        $this->assertEquals($expected, $actual);
199
200    }
201}
202