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_exclude_plugin_backlinks_test extends DokuWikiTest
27{
28
29    protected $pluginsEnabled = array('backlinks');
30
31    /**
32     * copy data.
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
62        if ($conf['allowdebug']) {
63            touch(DOKU_TMP_DATA . 'cache/debug.log');
64        }
65    }
66
67    public function tearDown(): void
68    {
69        parent::tearDown();
70
71        global $conf;
72        // try to get the debug log after running the test, print and clear
73        if ($conf['allowdebug']) {
74            print "\n";
75            readfile(DOKU_TMP_DATA . 'cache/debug.log');
76            unlink(DOKU_TMP_DATA . 'cache/debug.log');
77        }
78    }
79
80    public function testExclude(): void
81    {
82        $request = new TestRequest();
83        $response = $request->get(array('id' => 'backlinks_exclude_syntax'), '/doku.php');
84
85        $this->assertTrue(
86            strpos($response->getContent(), 'Backlinks to what Bob Ross says (excluding exclude namespace)') !== false,
87            '"Backlinks to what Bob Ross says (excluding exclude namespace)" was not in the output'
88        );
89
90        $this->assertTrue(
91            strpos($response->getContent(), 'An excluded link to Bob Ross') == false,
92            '"An excluded link to Bob Ross" should not be in the output'
93        );
94
95        $doc = phpQuery::newDocument($response->getContent());
96        // look for id="plugin__backlinks"
97        $this->assertEquals(
98            1,
99            pq('#plugin__backlinks', $doc)->length,
100            'There should be one backlinks element'
101        );
102
103        $wikilinks = pq('#plugin__backlinks ul li', $doc);
104        Logger::debug('found backlinks', $wikilinks->text());
105        $this->assertEquals(
106            3,
107            $wikilinks->contents()->length,
108            'There should be 3 backlinks'
109        );
110
111        $lastlink = pq('a:last', $wikilinks);
112        Logger::debug("last backlink", $lastlink->text());
113        $this->assertEquals(
114            'A link to Bob Ross',
115            $lastlink->text(),
116            'The last backlink should be "A link to Bob Ross"'
117        );
118    }
119}
120