1<?php
2
3
4/**
5 * Test the endpoint pages of the api plugin
6 *
7 * @group plugin_api
8 * @group plugins
9 * @uses \PHPUnit\Framework\TestCase
10 */
11include_once(__DIR__ . '/utils.php');
12
13class dokuwiki_plugin_api_pages_test extends DokuWikiTest
14{
15
16    const ENDPOINT_NAME = 'pages';
17
18    protected $pluginsEnabled = array(action_plugin_api::PLUGIN_NAME);
19    /**
20     * @var JSON
21     */
22    private static $JSON;
23
24    static function setUpBeforeClass(): void
25    {
26        self::$JSON = new JSON(JSON_LOOSE_TYPE);
27        $file = __DIR__ . '/../plugin.info.txt';
28        self::$PLUGIN_INFO = confToHash($file);
29
30    }
31
32    /**
33     * Test the pages function
34     */
35    public function test_plugin_pages()
36    {
37
38        // $conf must not be in a static method
39        global $conf;
40        // Use heading as title
41        $conf['useheading'] = 1;
42
43        /**
44         * Set things up
45         */
46        // Create a page
47        // The endpoint namespace is important to avoid race condition with other test
48        $homePageId = self::ENDPOINT_NAME . "home";
49        $summaryDefault = 'Summary';
50        saveWikiText($homePageId, 'Home Page', $summaryDefault);
51        idx_addPage($homePageId);
52
53        // A second page with a heading to test the titles
54        // The endpoint namespace is important to avoid race condition with other test
55        $secondPage = self::ENDPOINT_NAME . "PageToHome";
56        $secondPageTitle = 'Backlink Page Heading 1';
57        saveWikiText($secondPage, '====== ' . $secondPageTitle . '======' . DOKU_LF .
58            'Whatever', $summaryDefault);
59        idx_addPage($secondPage);
60
61
62        /**
63         * Query
64         */
65        $queryParameters = array(
66            'fn' => self::ENDPOINT_NAME
67        );
68        $response = dokuwiki_plugin_api_util::getRequest($queryParameters);
69        $data = self::$JSON->decode($response->getContent());
70
71        /**
72         * Test
73         */
74        // Two pages
75        // Due to concurrent test, we can have more than this two pages ...
76        // Wee check that they are in the index
77        $pagesFoundCounter = 0;
78        $pagesToFound = array($homePageId, $secondPage);
79        foreach ($data as $page) {
80
81
82            // Same Id
83
84            if (in_array($page['id'], $pagesToFound)) {
85                $pagesFoundCounter++;
86            }
87
88            if ($page['id'] == $homePageId) {
89                // Same Title
90                $actualPageTitle = $page['title'];
91                $expectedTitle = $homePageId;
92                $this->assertEquals($expectedTitle, $actualPageTitle, "A page title without header must be the page id");
93            }
94
95            if ($page['id'] ==$secondPage) {
96                $actualPageTitle = $page['title'];
97                $expectedTitle = $secondPageTitle;
98                $this->assertEquals($expectedTitle, $actualPageTitle, "A page title with header must be the first heading");
99            }
100
101        }
102        $this->assertEquals(sizeof($pagesToFound), $pagesFoundCounter, "The two pages were found");
103
104
105    }
106
107    /**
108     * Test the max pages parameters
109     */
110    public function test_plugin_pages_limit()
111    {
112
113
114        /**
115         * Set things up
116         */
117        // Create 10 pages
118        for ($i = 1; $i <= 10; $i++) {
119            saveWikiText($i, 'Text for the page ' . $i, "summary for page " . $i);
120            idx_addPage($i);
121        }
122
123        /**
124         * Query
125         */
126        $limit = 3;
127        $queryParameters = array(
128            'fn' => 'pages',
129            'limit' => $limit
130        );
131        $response = dokuwiki_plugin_api_util::getRequest($queryParameters);
132        $data = self::$JSON->decode($response->getContent());
133
134        /**
135         * Test
136         */
137        // Max pages
138        $this->assertEquals($limit, sizeof($data), "The number of page is equal t max");
139
140
141    }
142
143
144}
145