1<?php
2
3namespace dokuwiki\plugin\structtasks\test;
4
5use dokuwiki\plugin\struct\meta\AccessTable;
6use dokuwiki\plugin\struct\test\mock\Assignments;
7use dokuwiki\plugin\structtasks\meta\AssignedNotifier;
8
9use DokuWikiTest;
10
11/**
12 * Tests action handler for structtasks plugin
13 *
14 * @group plugin_structtasks
15 * @group plugins
16 */
17
18class action_plugin_structtasks_test extends StructtasksTest {
19
20    protected $pluginsEnabled = array('structtasks', 'struct', 'sqlite');
21
22    function testPageSaveNoAction() {
23        global $conf;
24        $conf['plugin']['structtasks']['schema'] = 'valid';
25        $this->loadSchemaJSON('valid', '', 100);
26        $action = plugin_load('action', 'structtasks');
27        $action->loadConfig();
28        $notifier = $this->createMock(AssignedNotifier::class);
29        $notifier->expects($this->never())->method('sendMessage');
30        $action->notifiers = [$notifier];
31        saveWikiText('some:page', 'test page content', 'saved for testing');
32    }
33
34    function testNoSchema() {
35        global $conf;
36        $conf['plugin']['structtasks']['schema'] = '';
37        $this->loadSchemaJSON('valid', '', 100);
38        $action = plugin_load('action', 'structtasks');
39        $action->loadConfig();
40        $notifier = $this->createMock(AssignedNotifier::class);
41        $notifier->expects($this->never())->method('sendMessage');
42        $action->notifiers = [$notifier];
43        saveWikiText('some:page', 'test page content', 'saved for testing');
44    }
45
46    function testPageSaveAction() {
47        global $auth;
48        $auth->createUser('user1', 'abcdefg', 'Arron Dom Person', 'adperson@example.com');
49        $auth->createUser('user2', '123456789', 'Fay Mail', 'user2@example.com');
50
51        global $conf;
52        $conf['plugin']['structtasks']['schema'] = 'valid';
53        $this->loadSchemaJSON('valid', '', 100);
54        $action = plugin_load('action', 'structtasks');
55        $action->loadConfig();
56
57        $page = 'some:page';
58        $page_title = 'Some Title';
59        $old_content = "====== ${page_title} ======\nInitial content";
60        $new_content = "====== ${page_title} ======\nNew content";
61        $old_data = ['duedate' => '2023-03-27',
62                     'assignees' => ['user1'],
63                     'status' => 'Ongoing'];
64        $new_data = ['duedate' => '2023-04-10',
65                     'assignees' => ['user1', 'user2'],
66                     'status' => 'Ongoing'];
67        $expected_old_data =[
68            'duedate' => date_create($old_data['duedate']),
69            'assignees' => ['Arron Dom Person <adperson@example.com>'],
70            'status' => $old_data['status'],
71            'content' => $old_content,
72            'duedate_formatted' => '27 Mar 2023',
73        ];
74        $expected_new_data = [
75            'duedate' => date_create($new_data['duedate']),
76            'assignees' => ['Arron Dom Person <adperson@example.com>',
77                            'Fay Mail <user2@example.com>'],
78            'status' => $new_data['status'],
79            'content' => $new_content,
80            'duedate_formatted' => '10 Apr 2023',
81        ];
82
83        $_SERVER['REMOTE_USER'] = 'user1';
84        $notifier = $this->createMock(AssignedNotifier::class);
85        $notifier->expects($this->once())
86                 ->method('sendMessage')
87                 ->with($this->equalTo($page),
88                        $this->equalTo($page_title),
89                        $this->equalTo('Arron Dom Person'),
90                        $this->equalTo('Arron Dom Person <adperson@example.com>'),
91                        $this->equalTo($expected_new_data),
92                        $this->equalTo($expected_old_data)
93                 );
94        $action->notifiers = [$notifier];
95
96        $access = AccessTable::getPageAccess('valid', $page, time());
97        $access->saveData($old_data);
98        $this->waitForTick();
99        saveWikiText($page, $old_content, 'save 1');
100        $assignments = Assignments::getInstance();
101        $assignments->assignPageSchema($page, 'valid');
102
103        $this->waitForTick();
104        $access = AccessTable::getPageAccess('valid', $page, time());
105        $access->saveData($new_data);
106        saveWikiText($page, $new_content, 'save 2');
107    }
108}
109