1<?php
2
3namespace dokuwiki\plugin\structtasks\test;
4
5use splitbrain\phpcli\Options;
6use dokuwiki\plugin\struct\meta\AccessTable;
7use dokuwiki\plugin\struct\test\mock\Assignments;
8use dokuwiki\plugin\structtasks\meta\ReminderNotifier;
9use dokuwiki\plugin\structtasks\meta\TodayNotifier;
10use dokuwiki\plugin\structtasks\meta\OverdueNotifier;
11
12use DokuWikiTest;
13
14/**
15 * Tests CLI-related methods for the structtasks plugin
16 *
17 * @group plugin_structtasks
18 * @group plugins
19 */
20
21class cli_plugin_structtasks_test extends StructtasksTest {
22    protected $pluginsEnabled = array('structtasks', 'struct', 'sqlite');
23
24    function testInitialiseSuccess() {
25        global $conf;
26        $conf['plugin']['structtasks']['schema'] = 'valid';
27        $conf['plugin']['structtasks']['reminder'] = '7,2,1,0';
28        $this->loadSchemaJSON('valid', '', 100);
29        $cli = plugin_load('cli', 'structtasks');
30        $this->assertTrue($cli->initialise());
31    }
32
33    function testInitialiseInvalidSchema() {
34        global $conf;
35        $conf['plugin']['structtasks']['schema'] = 'badstatus';
36        $this->loadSchemaJSON('badstatus', '', 100);
37        $cli = plugin_load('cli', 'structtasks');
38        $this->assertFalse($cli->initialise(false));
39    }
40
41    function testInitialiseMissingSchema() {
42        global $conf;
43        $conf['plugin']['structtasks']['schema'] = 'valid';
44        $cli = plugin_load('cli', 'structtasks');
45        $this->assertFalse($cli->initialise(false));
46    }
47
48    function testInitialiseNoSchema() {
49        global $conf;
50        $cli = plugin_load('cli', 'structtasks');
51        $this->assertFalse($cli->initialise());
52    }
53
54    function testCreateNotifiers() {
55        $cli = plugin_load('cli', 'structtasks');
56
57        $n = $cli->createNotifiers([3, 2, 1, 0], true);
58        $this->assertEquals(3, count($n));
59        $this->assertInstanceOf(TodayNotifier::class, $n[0]);
60        $this->assertInstanceOf(ReminderNotifier::class, $n[1]);
61        $this->assertEquals([3, 2, 1], $n[1]->getDaysBefore());
62        $this->assertInstanceOf(OverdueNotifier::class, $n[2]);
63
64        $n = $cli->createNotifiers([0], true);
65        $this->assertEquals(2, count($n));
66        $this->assertInstanceOf(TodayNotifier::class, $n[0]);
67        $this->assertInstanceOf(OverdueNotifier::class, $n[1]);
68
69        $n = $cli->createNotifiers([2, 1], false);
70        $this->assertEquals(1, count($n));
71        $this->assertInstanceOf(ReminderNotifier::class, $n[0]);
72        $this->assertEquals([2, 1], $n[0]->getDaysBefore());
73
74        $n = $cli->createNotifiers([], false);
75        $this->assertEquals(0, count($n));
76    }
77
78    function testProcessTask() {
79        global $auth;
80        $auth->createUser('user1', 'abcdefg', 'Some One', 'so@example.com');
81
82        global $conf;
83        $conf['plugin']['structtasks']['schema'] = 'valid';
84        $this->loadSchemaJSON('valid', '', 100);
85        $cli = plugin_load('cli', 'structtasks');
86        $this->assertTrue($cli->initialise());
87
88        $page = 'some:page';
89        $page_title = 'Some Title';
90        $content = "====== ${page_title} ======\nInitial content";
91        $data = ['duedate' => '2023-03-27',
92                 'assignees' => ['user1'],
93                 'status' => 'Ongoing'];
94        $expected_data =[
95            'duedate' => date_create($data['duedate']),
96            'assignees' => ['Some One <so@example.com>'],
97            'status' => $data['status'],
98            'content' => $content,
99            'duedate_formatted' => '27 Mar 2023',
100        ];
101        $access = AccessTable::getPageAccess('valid', $page, time());
102        $access->saveData($data);
103        saveWikiText($page, $content, 'save 1');
104
105        $notifier = $this->createMock(TodayNotifier::class);
106        $notifier->expects($this->once())
107                 ->method('sendMessage')
108                 ->with($this->equalTo($page),
109                        $this->equalTo($page_title),
110                        $this->equalTo(''),
111                        $this->equalTo(''),
112                        $this->equalTo($expected_data),
113                        $this->equalTo($expected_data)
114                 );
115
116        $assignments = Assignments::getInstance();
117        $assignments->assignPageSchema($page, 'valid');
118
119        $cli->processTask($page, [$notifier]);
120    }
121
122    function testRunCli() {
123        global $auth;
124        $auth->createUser('user1', 'abcdefg', 'Some One', 'so@example.com');
125
126        global $conf;
127        $conf['plugin']['structtasks']['schema'] = 'valid';
128        $this->loadSchemaJSON('valid', '', 100);
129        $cli = plugin_load('cli', 'structtasks');
130        $this->assertTrue($cli->initialise());
131
132        $assignments = Assignments::getInstance();
133        $data = ['duedate' => '2023-03-27',
134                 'assignees' => ['user1'],
135                 'status' => 'Ongoing'];
136
137        $page = [];
138        $page_title = [];
139        $content = [];
140        $expected_data = [];
141
142        $page[] = 'some:page';
143        $page_title[] = 'Some Title';
144        $content[] = "====== {$page_title[0]} ======\nInitial content";
145        $expected_data[] =[
146            'duedate' => date_create($data['duedate']),
147            'assignees' => ['Some One <so@example.com>'],
148            'status' => $data['status'],
149            'content' => $content[0],
150            'duedate_formatted' => '27 Mar 2023',
151        ];
152        $access = AccessTable::getPageAccess('valid', $page[0], time());
153        $access->saveData($data);
154        saveWikiText($page[0], $content[0], 'save 1');
155        $assignments->assignPageSchema($page[0], 'valid');
156
157        $page[] = 'another:page';
158        $page_title[] = 'Another Title';
159        $content[] = "====== {$page_title[1]} ======\nDifferent content";
160        $expected_data[] =[
161            'duedate' => date_create($data['duedate']),
162            'assignees' => ['Some One <so@example.com>'],
163            'status' => $data['status'],
164            'content' => $content[1],
165            'duedate_formatted' => '27 Mar 2023',
166        ];
167        $access = AccessTable::getPageAccess('valid', $page[1], time());
168        $access->saveData($data);
169        saveWikiText($page[1], $content[1], 'save 1');
170        $assignments->assignPageSchema($page[1], 'valid');
171
172        for ($i = 0; $i < 2; $i++) {
173            $notifier = $this->createMock(TodayNotifier::class);
174            $notifier->expects($this->exactly(2))
175                     ->method('sendMessage')
176                     ->withConsecutive([$this->equalTo($page[1]),
177                                        $this->equalTo($page_title[1]),
178                                        $this->equalTo(''),
179                                        $this->equalTo(''),
180                                        $this->equalTo($expected_data[1]),
181                                        $this->equalTo($expected_data[1])],
182                                       [$this->equalTo($page[0]),
183                                        $this->equalTo($page_title[0]),
184                                        $this->equalTo(''),
185                                        $this->equalTo(''),
186                                        $this->equalTo($expected_data[0]),
187                                        $this->equalTo($expected_data[0])]
188                     );
189            $cli->notifiers[] = $notifier;
190        }
191        $cli->testing = true;
192
193        $cli->notify(false);
194    }
195}
196