1<?php
2
3namespace dokuwiki\plugin\structtasks\test;
4
5use DokuWikiTest;
6use dokuwiki\plugin\structtasks\meta\Utilities;
7
8
9/**
10 * Tests for Utilities::isValidSchema.
11 *
12 * @group plugin_structtasks
13 * @group plugins
14 */
15
16class utilities_isvalid_plugin_structtasks_test extends StructtasksTest {
17    /**
18     * Create some useful properties.
19     */
20    public function setUp(): void {
21        parent::setUp();
22        $this->util = new Utilities(plugin_load('helper', 'struct'));
23    }
24
25    function validSchemas() {
26        return [['valid'], ['valid2']];
27    }
28
29    function invalidSchemas() {
30        return [['badassignees'], ['baddate'], ['badstatus'],
31                ['missingassignees'], ['missingdate'], ['missingstatus'],
32                ['multidate'], ['multistatus']];
33    }
34
35    /**
36     * @dataProvider validSchemas
37     */
38    function testIsValidSchema($schema) {
39        $this->assertFalse($this->util->isValidSchema($schema));
40        $this->loadSchemaJSON($schema);
41        $this->assertTrue($this->util->isValidSchema($schema));
42    }
43
44    /**
45     * @dataProvider invalidSchemas
46     */
47    function testIsInvalidSchema($schema) {
48        $this->assertFalse($this->util->isValidSchema($schema));
49        $this->loadSchemaJSON($schema);
50        $this->assertFalse($this->util->isValidSchema($schema));
51    }
52}
53
54
55/**
56 * Tests of the getMetadata method on the Utilities classes.
57 *
58 * @group plugin_structtasks
59 * @group plugins
60 */
61class utilities_metadata_plugin_structtasks_test extends StructtasksTest {
62
63    public function setUp(): void {
64        parent::setUp();
65        global $auth;
66        $this->util = new Utilities(plugin_load('helper', 'struct'));
67        $auth->createUser('user1', 'abcdefg', 'Arron Dom Person', 'adperson@example.com');
68        $auth->createUser('user2', '123456789', 'Fay Mail', 'user2@example.com');
69
70        $this->loadSchemaJSON('valid', '', 100);
71        $this->loadSchemaJSON('baddate', '', 100);
72        $this->rev1 = time() - 1;
73        $this->rev2 = time();
74        $this->rev3 = time() + 1;
75        $this->old_metadata = ['duedate' => '2023-03-26',
76                               'assignees' => ['user1'],
77                               'status' => 'Ongoing'];
78        $this->old_expected = ['duedate' => date_create('2023-03-26'),
79                               'assignees' => ['Arron Dom Person <adperson@example.com>'],
80                               'status' => 'Ongoing',
81                               'duedate_formatted' => '26 Mar 2023',
82        ];
83        $this->new_metadata = ['duedate' => '2023-04-10',
84                               'assignees' => ['user1', 'user2'],
85                               'status' => 'Ongoing'];
86        $this->new_expected = ['duedate' => date_create('2023-04-10'),
87                               'assignees' =>
88                               ['Arron Dom Person <adperson@example.com>',
89                                'Fay Mail <user2@example.com>'],
90                               'status' => 'Ongoing',
91                               'duedate_formatted' => '10 Apr 2023',
92        ];
93        $this->nodate_metadata = ['duedate' => '',
94                                  'assignees' => ['user1'],
95                                  'status' => 'Ongoing'];
96        $this->nodate_expected = ['duedate' => null,
97                                  'assignees' => ['Arron Dom Person <adperson@example.com>'],
98                                  'status' => 'Ongoing',
99                                  'duedate_formatted' => '',
100        ];
101        $this->saveData('some:page', 'valid',
102                        $this->old_metadata,
103                        $this->rev1);
104        $this->saveData('some:page', 'valid',
105                        $this->new_metadata,
106                        $this->rev2);
107        $this->saveData('some:page', 'valid',
108                        $this->nodate_metadata,
109                        $this->rev3);
110        saveWikiText('another:page', 'page without a task', 'saved for testing');
111     }
112
113    function testGetMetadata() {
114        list($old_data, $new_data, $valid) = $this->util->getMetadata(
115            'some:page', 'valid', $this->rev1, $this->rev2);
116        $this->assertTrue($valid);
117        foreach ($this->old_expected as $key => $val) {
118            $this->assertEquals($old_data[$key], $val);
119        }
120        foreach ($this->new_expected as $key => $val) {
121            $this->assertEquals($new_data[$key], $val);
122        }
123    }
124
125    function testGetMetadataNoDate() {
126        list($old_data, $new_data, $valid) = $this->util->getMetadata(
127            'some:page', 'valid', $this->rev1, $this->rev3);
128        $this->assertTrue($valid);
129        foreach ($this->old_expected as $key => $val) {
130            $this->assertEquals($old_data[$key], $val);
131        }
132        foreach ($this->nodate_expected as $key => $val) {
133            $this->assertEquals($new_data[$key], $val);
134        }
135    }
136
137    function invalidMetadataProvider() {
138        return [
139            'No data for page' => ['another:page', 'valid'],
140            'Page does not exist' => ['not:a:page', 'valid'],
141            'Unsuitable schema, no data' => ['another:page', 'baddate'],
142            'Unsuitable schema, not assigned' => ['some:page', 'baddate'],
143            'Schema does not exist' => ['another:page', 'does_not_exist'],
144        ];
145    }
146
147    /**
148     * @dataProvider invalidMetadataProvider
149     */
150    function testGetMetadataInvalid($page, $schema) {
151        list($old_data, $new_data, $valid) = $this->util->getMetadata($page, $schema, $this->rev1, $this->rev2);
152        $this->assertNull($old_data);
153        $this->assertNull($new_data);
154        $this->assertFalse($valid);
155    }
156}
157
158
159/**
160 * Tests the remaining methods on the Utilities classes, which don't
161 * require setting up the database.
162 *
163 * @group plugin_structtasks
164 * @group plugins
165 */
166class utilities_simple_plugin_structtakss_test extends \DokuWikiTest {
167
168    function setUp() : void {
169        parent::setUp();
170        global $auth;
171        $this->util = new Utilities(plugin_load('helper', 'struct'));
172        $auth->createUser('user1', 'abcdefg', 'Arron Dom Person', 'adperson@example.com');
173        $auth->createUser('user2', '123456789', 'Fay Mail', 'user2@example.com');
174        $auth->createUser('user3', 'asdkfjdl', '', 'some@mail.com');
175    }
176
177    function testGetUserEmail() {
178        $this->assertEquals('Arron Dom Person <adperson@example.com>',
179                            $this->util->getUserEmail('user1'));
180        $this->assertEquals('Fay Mail <user2@example.com>',
181                            $this->util->getUserEmail('user2'));
182        $this->assertEquals('some@mail.com',
183                            $this->util->getUserEmail('user3'));
184        $this->assertEquals('', $this->util->getUserEmail('DoesNotExist'));
185        $this->assertEquals('raw.email@address.org',
186                            $this->util->getUserEmail('raw.email@address.org'));
187        $this->assertEquals('user2@example.com',
188                            $this->util->getUserEmail('user2@example.com'));
189    }
190}
191