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_include_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 testInclude(): void
81    {
82        $request  = new TestRequest();
83        $response = $request->get(array('id' => 'backlinks_include_syntax'));
84
85        $this->assertTrue(
86            str_contains($response->getContent(), 'Backlinks to what Bob Ross says (including only)'),
87            '"Backlinks to what Bob Ross says (including only)" was not in the output'
88        );
89
90        $doc = phpQuery::newDocument($response->getContent());
91        // look for id="plugin__backlinks"
92        $this->assertEquals(
93            1,
94            pq('#plugin__backlinks', $doc)->length,
95            'There should be one backlinks element'
96        );
97
98        $wikilinks = pq('#plugin__backlinks ul li', $doc);
99        Logger::debug('found backlinks', $wikilinks->text());
100        $this->assertEquals(
101            1,
102            $wikilinks->contents()->length,
103            'There should be 1 backlink'
104        );
105
106        $lastlink = pq('a:last', $wikilinks);
107        Logger::debug("last backlink", $lastlink->text());
108        $this->assertEquals(
109            'An included link to Bob Ross',
110            $lastlink->text(),
111            'The last backlink should be "An included link to Bob Ross"'
112        );
113    }
114}
115