1<?php
2
3namespace dokuwiki\plugin\lms\test;
4
5use DokuWikiTest;
6
7/**
8 * Helper tests for the lms plugin
9 *
10 * @group plugin_lms
11 * @group plugins
12 */
13class HelperTest extends DokuWikiTest
14{
15    public function setUp(): void
16    {
17        parent::setUp();
18        saveWikiText('lms', file_get_contents(__DIR__ . '/data/pages/lms.txt'), 'test');
19        saveWikiText('foo:lms', file_get_contents(__DIR__ . '/data/pages/lms.txt'), 'test');
20    }
21
22    public function testMarkRead()
23    {
24        $hlp = new \helper_plugin_lms();
25
26        // no lessons, yet
27        $this->assertEquals(
28            [],
29            array_keys($hlp->getUserLessons('test'))
30        );
31
32        // add lesson
33        $hlp->markLesson('foo', 'test', true);
34        $this->assertEquals(
35            ['foo'],
36            array_keys($hlp->getUserLessons('test'))
37        );
38
39        // add second lesson
40        $hlp->markLesson('bar', 'test', true);
41        $this->assertEquals(
42            ['foo', 'bar'],
43            array_keys($hlp->getUserLessons('test'))
44        );
45
46        // unmark first lesson
47        $hlp->markLesson('foo', 'test', false);
48        $this->assertEquals(
49            ['bar'],
50            array_keys($hlp->getUserLessons('test'))
51        );
52    }
53
54    public function testNextLesson() {
55        $hlp = new \helper_plugin_lms();
56
57        $id = 'nope';
58        $this->setIdGlobals($id);
59        $result = $hlp->getNextLesson($id);
60        $this->assertEquals(false, $result, '$id is not a lesson');
61
62        $id = 'link';
63        $this->setIdGlobals($id);
64        $result = $hlp->getNextLesson($id);
65        $this->assertEquals(false, $result, '$id is last lesson');
66
67        $id = 'this';
68        $this->setIdGlobals($id);
69        $result = $hlp->getNextLesson($id);
70        $this->assertEquals('foo:bar', $result, 'next lesson no user context');
71
72        $hlp->markLesson('foo:bar', 'test', true);
73        $result = $hlp->getNextLesson($id, 'test');
74        $this->assertEquals('another_link', $result, 'skip seen lesson');
75    }
76
77    public function testPrevLesson() {
78        $hlp = new \helper_plugin_lms();
79
80        $id = 'nope';
81        $this->setIdGlobals($id);
82        $result = $hlp->getPrevLesson('nope');
83        $this->assertEquals(false, $result, '$id is not a lesson');
84
85        $id = 'this';
86        $this->setIdGlobals($id);
87        $result = $hlp->getPrevLesson('this');
88        $this->assertEquals(false, $result, '$id is first lesson');
89
90        $id = 'another_link';
91        $this->setIdGlobals($id);
92        $result = $hlp->getPrevLesson($id);
93        $this->assertEquals('foo:bar', $result, 'prev lesson no user context');
94
95        $hlp->markLesson('foo:bar', 'test', true);
96        $result = $hlp->getPrevLesson('another_link', 'test');
97        $this->assertEquals('this', $result, 'skip seen lesson');
98    }
99
100    public function testParseTopControlPage()
101    {
102        $hlp = new \helper_plugin_lms();
103
104        $result = $this->callInaccessibleMethod($hlp, 'parseControlPage', ['lms']);
105        $expected = [
106            'this',
107            'foo:bar',
108            'another_link',
109            'relativeup',
110            'foo2:this',
111            'blarg:down',
112            'toplevel',
113            'link',
114        ];
115
116        $this->assertEquals($expected, $result);
117    }
118
119    public function testParseNsControlPage()
120    {
121        $hlp = new \helper_plugin_lms();
122
123        $result = $this->callInaccessibleMethod($hlp, 'parseControlPage', ['foo:lms']);
124        $expected = [
125            'foo:this',
126            'foo:bar',
127            'foo:another_link',
128            'foo:blarg:down',
129            'foo:link',
130        ];
131
132        $this->assertEquals($expected, $result);
133    }
134
135    public function testParseControlPageMissing()
136    {
137        $hlp = new \helper_plugin_lms();
138
139        $result = $this->callInaccessibleMethod($hlp, 'parseControlPage', ['nope']);
140        $expected = [];
141
142        $this->assertEquals($expected, $result);
143    }
144
145    /**
146     * @param string $id
147     * @return void
148     */
149    protected function setIdGlobals($id)
150    {
151        global $ID;
152        global $INFO;
153        $ID = $id;
154        $INFO['id'] = $ID;
155    }
156}
157