1<?php
2
3if (!defined('DW_LF')) {
4    define('DW_LF', "\n");
5}
6
7require_once(__DIR__ . '/../webcomponent.php');
8/**
9 * Test the related plugin
10 *
11 * @group plugin_webcomponent
12 * @group plugins
13 */
14class dokuwiki_plugin_webcomponent_related_test extends DokuWikiTest
15{
16
17    protected $pluginsEnabled = array('webcomponent');
18
19    // Namespace where all test page will be added
20    const TEST_PAGE_NAMESPACE = webcomponent::PLUGIN_NAME . 'test:';
21    const REFERENT_PAGE_ID = self::TEST_PAGE_NAMESPACE . 'referent';
22    public static $referrers = array();
23
24    const REFERRERS_COUNT = 4;
25
26    // for the extra pattern test
27    const EXTRA_PATTERN_VALUE = '{{backlinks>.}}';
28    public static $extraPatternPage;
29
30    // The value of the REFERRERS_ID_TOP is the referrers that will
31    // got two backlinks and should therefore be on the top in a list of related page
32    // This number should be less that REFERRERS_COUNT
33    const REFERRERS_ID_TOP = 3;
34
35    // Create the pages
36    // and configure
37    public static function setUpBeforeClass(): void
38    {
39        parent::setUpBeforeClass();
40        // Config changes in function setUpBeforeClass() have no effect set setup
41
42        // Create the pages
43        $changeSummary = 'Test - Component related';
44
45        // Create the referent page
46        $referentPageId = self::REFERENT_PAGE_ID;
47        saveWikiText($referentPageId,
48            '======  A referent page ====== ' . DW_LF . DW_LF .
49            '=====  Articles Related ====== ' . DW_LF .
50            '<' . syntax_plugin_webcomponent_related::getElementName() . '>'
51            , $changeSummary);
52        idx_addPage($referentPageId);
53
54        // Create the referrers page with a link to the referent page
55        for ($i = 1; $i <= self::REFERRERS_COUNT; $i++) {
56            self::createReferrerPage($referentPageId);
57        }
58
59        // Create a referrer page that links to the first referrers
60        // It should then be the first one in the related list
61        if (self::REFERRERS_ID_TOP > self::REFERRERS_COUNT) {
62            throw new Exception("The value of the REFERRERS_ID_TOP (" . self::REFERRERS_ID_TOP . ") should be less than the value of REFERRERS_COUNT (" . self::REFERRERS_COUNT . ")");
63        }
64        self::createReferrerPage(self::$referrers[self::REFERRERS_ID_TOP]);
65
66        // Extra Pattern Page
67        $PageId = 'extraPatternTest';
68        self::$extraPatternPage = self::TEST_PAGE_NAMESPACE . $PageId;
69        saveWikiText(self::$extraPatternPage,
70            '======  ' . $PageId . ' ======' . DW_LF . DW_LF .
71            self::EXTRA_PATTERN_VALUE . DW_LF . DW_LF .
72            '<' . syntax_plugin_webcomponent_related::getElementName() . '>', $changeSummary);
73        idx_addPage(self::$extraPatternPage);
74        self::createReferrerPage(self::$extraPatternPage);
75
76        // A Home page to be able to test visually
77        $startId = self::TEST_PAGE_NAMESPACE . 'start';
78        $referrersWiki = "";
79        foreach (self::$referrers as $referrer) {
80            $referrersWiki .= '  * [[' . $referrer . ']]' . DW_LF;
81        }
82        saveWikiText($startId, '====== The related home page ======' . DW_LF . DW_LF .
83            '  * [[' . $referentPageId . ']]' . DW_LF .
84            $referrersWiki .
85            '  * [[' . self::$extraPatternPage . ']]' . DW_LF
86            , $changeSummary);
87
88
89        dbglog("\nTest Plugin" . webcomponent::PLUGIN_NAME.".".syntax_plugin_webcomponent_related::getElementName() . ': Start Page was created at ' . wikiFN($startId));
90
91
92    }
93
94    /**
95     * @param $referentPageId - The Full referrant page id
96     * @return string
97     */
98    public static function createReferrerPage($referentPageId): string
99    {
100        $referrerId = sizeof(self::$referrers) + 1;
101        $referrerPageId = self::TEST_PAGE_NAMESPACE . 'referrer' . $referrerId;
102        saveWikiText($referrerPageId,
103            '======   Referrer ' . $referrerId . ' to ' . $referentPageId . ' ======' . DW_LF . DW_LF .
104            '  * [[' . $referentPageId . ']]', "Test");
105        idx_addPage($referrerPageId);
106        self::$referrers[] = $referrerPageId;
107        return $referrerPageId;
108    }
109
110    public function setUp()
111    {
112        // Config changes have only effect in function setUpBeforeClass()
113        global $conf;
114
115        parent::setUp();
116
117        webcomponent::setUpPagesLocation();
118
119        $conf ['plugin'][webcomponent::PLUGIN_NAME][syntax_plugin_webcomponent_related::EXTRA_PATTERN_CONF] = self::EXTRA_PATTERN_VALUE;
120
121        dbglog("\nSetup was called- Test Plugin" . webcomponent::PLUGIN_NAME." - Compo".syntax_plugin_webcomponent_related::getElementName());
122
123    }
124
125
126
127    // Test the dokuwiki backlinks function
128    public function test_backlinks()
129    {
130
131        $backlinks = ft_backlinks(self::REFERENT_PAGE_ID);
132        $this->assertEquals(self::REFERRERS_COUNT, sizeof($backlinks));
133
134    }
135
136    // Test the related features of the related function
137    // default, max and order
138    public function test_BaseRelated()
139    {
140        // Without max
141        $referentPageId = self::REFERENT_PAGE_ID;
142        $relatedPlugin = new syntax_plugin_webcomponent_related();
143        // Without max, it will take the conf default (10)
144        $referrers = $relatedPlugin->related($referentPageId);
145        $this->assertEquals(self::REFERRERS_COUNT, sizeof($referrers));
146        // The first one must be the one that had two backlinks
147        $this->assertEquals(self::$referrers[self::REFERRERS_ID_TOP], $referrers[0][syntax_plugin_webcomponent_related::RELATED_PAGE_ID_PROP]);
148
149        // With a max via argument
150        $max = 1;
151        $referrers = $relatedPlugin->related($referentPageId, $max);
152        $expected = $max + 1; // +1 for the more page
153        $this->assertEquals($expected, sizeof($referrers));
154
155        // With a max via the conf
156        global $conf;
157        $oldMaxLinksValue = $conf ['plugin'][webcomponent::PLUGIN_NAME][syntax_plugin_webcomponent_related::MAX_LINKS_CONF];
158        $conf ['plugin'][webcomponent::PLUGIN_NAME][syntax_plugin_webcomponent_related::MAX_LINKS_CONF] = $max;
159        $referrers = $relatedPlugin->related($referentPageId);
160        $this->assertEquals($expected, sizeof($referrers));
161        $conf ['plugin'][webcomponent::PLUGIN_NAME][syntax_plugin_webcomponent_related::MAX_LINKS_CONF] = $oldMaxLinksValue;
162
163    }
164
165    public function test_extraPattern()
166    {
167
168
169        $request = new TestRequest();
170        $request->get(array('id' => self::$extraPatternPage));
171        $response = $request->execute();
172
173        //$response->queryHTML('#'.syntax_plugin_related::ELEMENT_ID)->attr('content');
174        $idElements = $response->queryHTML('#' . syntax_plugin_webcomponent_related::getElementId())->length;
175        $this->assertEquals(2, $idElements);
176
177    }
178
179
180}
181