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