1<?php
2
3
4/**
5 * Test the endpoint page 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_page_test extends DokuWikiTest
14{
15
16    const ENDPOINT_NAME = 'page';
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_page()
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, 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        // Backlinks
54        // The endpoint namespace is important to avoid race condition with other test
55        $backlinkHomePageId = self::ENDPOINT_NAME."PageWithLinkToHome";
56        $externalLink = 'https://gerardnico.com';
57        $backlinkHomePageTitle = 'Backlink Page Heading 1';
58        saveWikiText($backlinkHomePageId, '====== ' . $backlinkHomePageTitle . '======' . DOKU_LF .
59            '[['.$homePageId.']] - [[' . $externalLink . ']]', $summaryDefault);
60        idx_addPage($backlinkHomePageId);
61
62
63        /**
64         * Query Home page
65         */
66        $queryParameters = array(
67            'fn' => self::ENDPOINT_NAME,
68            'id' => $homePageId
69        );
70        $response = dokuwiki_plugin_api_util::getRequest($queryParameters);
71        $data = self::$JSON->decode($response->getContent());
72
73        /**
74         * Test
75         */
76
77
78        // Same Title
79        $actualPageTitle = $data['title'];
80        $expectedTitle = $homePageId;
81        $this->assertEquals($expectedTitle, $actualPageTitle, "A page title without header must be the page id");
82
83        // Html
84        $expectedHtml = DOKU_LF . '<p>' . DOKU_LF . 'Home Page' . DOKU_LF . '</p>' . DOKU_LF;
85        $actualHtml = $data['html'];
86        $this->assertEquals($expectedHtml, $actualHtml, "The Html must be the same");
87
88        // backlinks
89        $actualBacklinks = $data['backlinks'];
90        $expectedBacklinks = array(
91            0 => $backlinkHomePageId
92        );
93        $this->assertEquals($expectedBacklinks, $actualBacklinks, "The Backlinks must be the same");
94
95        /**
96         * Query Second page
97         */
98        $queryParameters = array(
99            'fn' => 'page',
100            'id' => $backlinkHomePageId
101        );
102        $response = dokuwiki_plugin_api_util::getRequest($queryParameters);
103        $data = self::$JSON->decode($response->getContent());
104
105        // Title
106        $actualPageTitle = $data['title'];
107        $expectedTitle = $backlinkHomePageTitle;
108        $this->assertEquals($expectedTitle, $actualPageTitle, "A page title with header must be the first heading");
109
110        // internal links
111        $actualLinks = $data['links'];
112        $expectedLinks = array(
113            0 => $homePageId
114        );
115        $this->assertEquals($expectedLinks, $actualLinks, "The links must be the same");
116
117        // external links
118        $actualExternalLinks = $data['external_links'];
119        $expectedExternalLinks = array(
120            0 => $externalLink
121        );
122        $this->assertEquals($expectedExternalLinks, $actualExternalLinks, "The externals links must be the same");
123
124
125    }
126
127}
128