1<?php
2/*
3 * Copyright (c) 2016 Mark C. Prins <mprins@users.sf.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18use dokuwiki\Logger;
19
20/**
21 * Syntax tests for the backlinks plugin.
22 *
23 * @group plugin_backlinks
24 * @group plugins
25 */
26class syntax_plugin_backlinks_test extends DokuWikiTest
27{
28
29    protected $pluginsEnabled = array('backlinks');
30
31    /**
32     * copy data and add pages to the index.
33     */
34    public static function setUpBeforeClass(): void
35    {
36        parent::setUpBeforeClass();
37        global $conf;
38        $conf['allowdebug'] = 1;
39
40        TestUtils::rcopy(TMP_DIR, dirname(__FILE__) . '/data/');
41
42        Logger::debug("set up class syntax_plugin_backlinks_test");
43    }
44
45    public function setUp(): void
46    {
47        parent::setUp();
48
49        global $conf;
50        $conf['allowdebug'] = 1;
51        $conf['cachetime'] = -1;
52        $verbose = false;
53        $force = false;
54
55        $data = array();
56        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
57
58        foreach ($data as $val) {
59            idx_addPage($val['id'], $verbose, $force);
60        }
61        //idx_addPage('bob_ross_says', $verbose, $force);
62        //idx_addPage('link', $verbose, $force);
63        //idx_addPage('backlinks_syntax', $verbose, $force);
64        if ($conf['allowdebug']) {
65            touch(DOKU_TMP_DATA . 'cache/debug.log');
66        }
67    }
68
69    public function tearDown(): void
70    {
71        parent::tearDown();
72
73        global $conf;
74        // try to get the debug log after running the test, print and clear
75        if ($conf['allowdebug']) {
76            print "\n";
77            readfile(DOKU_TMP_DATA . 'cache/debug.log');
78            unlink(DOKU_TMP_DATA . 'cache/debug.log');
79        }
80    }
81
82    public function testIndex(): void
83    {
84        $query = array('ross');
85        $this->assertEquals(
86            array(
87                'ross' => array(
88                    'link' => '3',
89                    'bob_ross_says' => '1',
90                    'backlinks_syntax' => '2',
91                    'backlinks_include_syntax' => '2',
92                    'backlinks_exclude_syntax' => '2',
93                    'backlink_test_pages' => '8',
94                    'include:link' => '3',
95                    'exclude:link' => '3'
96                )
97            ),
98            idx_lookup($query)
99        );
100    }
101
102    public function testLinksPage(): void
103    {
104        $request = new TestRequest();
105        $response = $request->get(array('id' => 'link'), '/doku.php');
106
107        $this->assertTrue(
108            str_contains($response->getContent(), 'A link to Bob Ross'),
109            '"A link to Bob Ross" was not in the output'
110        );
111    }
112
113    public function testStoryPage(): void
114    {
115        $request = new TestRequest();
116        $response = $request->get(array('id' => 'bob_ross_says'), '/doku.php');
117
118        $this->assertTrue(
119            str_contains($response->getContent(), 'Bob Ross says'),
120            '"Bob Ross says" was not in the output'
121        );
122    }
123
124    public function testBacklinks(): void
125    {
126        $request = new TestRequest();
127        $response = $request->get(array('id' => 'backlinks_syntax'), '/doku.php');
128
129        $this->assertTrue(
130            str_contains($response->getContent(), 'Backlinks to what Bob Ross says'),
131            '"Backlinks to what Bob Ross says" was not in the output'
132        );
133
134        $doc = phpQuery::newDocument($response->getContent());
135        // look for id="plugin__backlinks"
136        $this->assertEquals(
137            1,
138            pq('#plugin__backlinks', $doc)->length,
139            'There should be one backlinks element'
140        );
141
142        $wikilinks = pq('#plugin__backlinks ul li', $doc);
143        Logger::debug('found backlinks', $wikilinks->text());
144        $this->assertEquals(
145            4,
146            $wikilinks->contents()->length,
147            'There should be 4 backlinks'
148        );
149
150        $lastlink = pq('a:last', $wikilinks);
151        Logger::debug("last backlink", $lastlink->text());
152        $this->assertEquals(
153            'A link to Bob Ross',
154            $lastlink->text(),
155            'The last backlink should be a link to Bob Ross'
156        );
157    }
158}
159