xref: /plugin/acknowledge/_test/HelperTest.php (revision 5966046c9f2860981ac6f31f94d13d9b1dc9b700) !
1<?php
2
3
4namespace dokuwiki\plugin\acknowledge\test;
5
6use DokuWikiTest;
7
8/**
9 * Helper tests for the acknowledge plugin
10 *
11 * @group plugin_acknowledge
12 * @group plugins
13 */
14class HelperTest extends DokuWikiTest
15{
16    /** @var array */
17    protected $pluginsEnabled = ['acknowledge', 'sqlite'];
18    /** @var \helper_plugin_acknowledge $helper */
19    protected $helper;
20    /** @var \helper_plugin_sqlite */
21    protected $db;
22
23    public static function setUpBeforeClass(): void
24    {
25        parent::setUpBeforeClass();
26        /** @var \auth_plugin_authplain $auth */
27        global $auth;
28        $auth->createUser('max', 'none', 'max', 'max@example.com', ['super']);
29    }
30
31    public function setUp(): void
32    {
33        parent::setUp();
34        $this->helper = plugin_load('helper', 'acknowledge');
35
36        $this->db = $this->helper->getDB();
37
38        $pages = "REPLACE INTO pages(page,lastmod)
39            VALUES ('dokuwiki:acktest1', 1560805365),
40            ('dokuwiki:acktest2', 1560805365),
41            ('dokuwiki:acktest3', 1560805365)";
42        $this->db->query($pages);
43
44        $assignments = "REPLACE INTO assignments(page,pageassignees)
45            VALUES ('dokuwiki:acktest1', 'regular, @super'),
46            ('dokuwiki:acktest2', '@super'),
47            ('dokuwiki:acktest3', '@user')";
48        $this->db->query($assignments);
49
50        // outdated, current, outdated but replaced, current replacing outdated, outdated
51        $acks = "REPLACE INTO acks(page,user,ack)
52            VALUES ('dokuwiki:acktest3', 'regular', 1550801270),
53            ('dokuwiki:acktest3', 'regular', 1560805555),
54            ('dokuwiki:acktest1', 'max', 1550805770),
55            ('dokuwiki:acktest1', 'max', 1560805770),
56            ('dokuwiki:acktest3', 'max', 1560805000)
57            ";
58        $this->db->query($acks);
59    }
60
61    /**
62     * test latest acknowledgements
63     */
64    public function test_getLatestAcknowledgements()
65    {
66        $actual = $this->helper->getAcknowledgements();
67        $expected = [
68            [
69                'page' => 'dokuwiki:acktest1',
70                'user' => 'max',
71                'ack' => '1560805770',
72                'lastmod' => '1560805365',
73            ],
74            [
75                'page' => 'dokuwiki:acktest3',
76                'user' => 'regular',
77                'ack' => '1560805555',
78                'lastmod' => '1560805365',
79            ],
80            [
81                'page' => 'dokuwiki:acktest3',
82                'user' => 'max',
83                'ack' => '1560805000',
84                'lastmod' => '1560805365',
85            ],
86        ];
87        $this->assertEquals($expected, $actual);
88    }
89
90    /**
91     * test latest acknowledgements limited to 1
92     */
93    public function test_getLimitedAcknowledgements()
94    {
95        $actual = $this->helper->getAcknowledgements(1);
96        $expected = [
97            [
98                'page' => 'dokuwiki:acktest1',
99                'user' => 'max',
100                'ack' => '1560805770',
101                'lastmod' => '1560805365',
102            ],
103        ];
104        $this->assertEquals($expected, $actual);
105    }
106
107    /**
108     * test assignment query
109     */
110    public function test_getUserAssignments()
111    {
112        $actual = $this->helper->getUserAssignments('regular', ['user']);
113        $expected = [
114            [
115                'page' => 'dokuwiki:acktest1',
116                'pageassignees' => 'regular, @super',
117                'autoassignees' => '',
118                'lastmod' => '1560805365',
119                'user' => null,
120                'ack' => null,
121            ],
122        ];
123        $this->assertEquals($expected, $actual);
124
125        $actual = $this->helper->getUserAssignments('max', ['user', 'super']);
126        $expected = [
127            [
128                'page' => 'dokuwiki:acktest2',
129                'pageassignees' => '@super',
130                'autoassignees' => '',
131                'lastmod' => '1560805365',
132                'user' => null,
133                'ack' => null,
134            ],
135            [
136                'page' => 'dokuwiki:acktest3',
137                'pageassignees' => '@user',
138                'autoassignees' => '',
139                'lastmod' => '1560805365',
140                'user' => null,
141                'ack' => null,
142            ],
143        ];
144        $this->assertEquals($expected, $actual);
145    }
146
147    /**
148     * Test all acknowledgements for a user (done or still due)
149     *
150     * @return void
151     */
152    public function test_getUserAcknowledgementsAll()
153    {
154        $actual = $this->helper->getUserAcknowledgements('max', ['user', 'super']);
155        $expected = [
156            // current / up to date
157            [
158                'page' => 'dokuwiki:acktest1',
159                'pageassignees' => 'regular, @super',
160                'autoassignees' => '',
161                'lastmod' => '1560805365',
162                'user' => 'max',
163                'ack' => '1560805770',
164            ],
165            // due / missing
166            [
167                'page' => 'dokuwiki:acktest2',
168                'pageassignees' => '@super',
169                'autoassignees' => '',
170                'lastmod' => '1560805365',
171                'user' => null,
172                'ack' => null,
173            ],
174            // outdated
175            [
176                'page' => 'dokuwiki:acktest3',
177                'pageassignees' => '@user',
178                'autoassignees' => '',
179                'lastmod' => '1560805365',
180                'user' => 'max',
181                'ack' => '1560805000',
182            ],
183        ];
184        $this->assertEquals($expected, $actual);
185    }
186
187    /**
188     * Test pages that user still has to acknowledge
189     *
190     * @return void
191     */
192    public function test_getUserAcknowledgementsDue()
193    {
194        $actual = $this->helper->getUserAcknowledgements('max', ['user', 'super'], 'due');
195        $expected = [
196            [
197                'page' => 'dokuwiki:acktest2',
198                'pageassignees' => '@super',
199                'autoassignees' => '',
200                'lastmod' => '1560805365',
201                'user' => null,
202                'ack' => null,
203            ],
204            [
205                'page' => 'dokuwiki:acktest3',
206                'pageassignees' => '@user',
207                'autoassignees' => '',
208                'lastmod' => '1560805365',
209                'user' => 'max',
210                'ack' => '1560805000',
211            ],
212        ];
213        $this->assertEquals($expected, $actual);
214    }
215
216    /**
217     * Test current / up-to-date acknowledgements
218     *
219     * @return void
220     */
221    public function test_getUserAcknowledgementsCurrent()
222    {
223        $actual = $this->helper->getUserAcknowledgements('max', ['user', 'super'], 'current');
224        $expected = [
225            [
226                'page' => 'dokuwiki:acktest1',
227                'pageassignees' => 'regular, @super',
228                'autoassignees' => '',
229                'lastmod' => '1560805365',
230                'user' => 'max',
231                'ack' => '1560805770',
232            ],
233        ];
234        $this->assertEquals($expected, $actual);
235    }
236
237    /**
238     * Test outdated acknowledgements (ack exists, but for older page revision)
239     *
240     * @return void
241     */
242    public function test_getUserAcknowledgementsOutdated()
243    {
244        $actual = $this->helper->getUserAcknowledgements('max', ['user', 'super'], 'outdated');
245        $expected = [
246            [
247                'page' => 'dokuwiki:acktest3',
248                'pageassignees' => '@user',
249                'autoassignees' => '',
250                'lastmod' => '1560805365',
251                'user' => 'max',
252                'ack' => '1560805000',
253            ],
254        ];
255        $this->assertEquals($expected, $actual);
256    }
257
258    /**
259     * Check what users are assigned to a page that has a user and a group in the database
260     */
261    public function test_getPageAssignees()
262    {
263        $actual = $this->helper->getPageAssignees('dokuwiki:acktest1');
264        $expected = ['regular', 'max'];
265        $this->assertEquals($expected, $actual);
266    }
267
268    /**
269     * Check what acknowledgments are there for a page
270     */
271    public function test_getPageAcknowledgements()
272    {
273        $actual = $this->helper->getPageAcknowledgements('dokuwiki:acktest1');
274        $expected = [
275            [
276                'page' => 'dokuwiki:acktest1',
277                'lastmod' => '1560805365',
278                'user' => 'max',
279                'ack' => '1560805770',
280            ],
281            [
282                'page' => 'dokuwiki:acktest1',
283                'lastmod' => '1560805365',
284                'user' => 'regular',
285                'ack' => null,
286            ],
287
288        ];
289        $this->assertEquals($expected, $actual);
290
291    }
292}
293