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
18/**
19 * Syntax tests for the backlinks plugin.
20 *
21 * @group plugin_backlinks
22 * @group plugins
23 */
24class syntax_plugin_backlinks_test extends DokuWikiTest {
25
26    protected $pluginsEnabled = array('backlinks');
27
28    /**
29     * copy data and add pages to the index.
30     */
31    public static function setUpBeforeClass(): void {
32        parent::setUpBeforeClass();
33        global $conf;
34        $conf['allowdebug'] = 1;
35
36        TestUtils::rcopy(TMP_DIR, dirname(__FILE__) . '/data/');
37
38        dbglog("\nset up class syntax_plugin_backlinks_test");
39    }
40
41    function setUp(): void {
42        parent::setUp();
43
44        global $conf;
45        $conf['allowdebug'] = 1;
46        $conf['cachetime']  = -1;
47
48        $data = array();
49        search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
50
51        //dbglog($data, "pages for indexing");
52
53        $verbose = false;
54        $force   = false;
55        foreach($data as $val) {
56            idx_addPage($val['id'], $verbose, $force);
57        }
58        //idx_addPage('bob_ross_says', $verbose, $force);
59        //idx_addPage('link', $verbose, $force);
60        //idx_addPage('backlinks_syntax', $verbose, $force);
61        if($conf['allowdebug']) {
62            touch(DOKU_TMP_DATA . 'cache/debug.log');
63        }
64    }
65
66    public function tearDown(): void {
67        parent::tearDown();
68
69        global $conf;
70        // try to get the debug log after running the test, print and clear
71        if($conf['allowdebug']) {
72            print "\n";
73            readfile(DOKU_TMP_DATA . 'cache/debug.log');
74            unlink(DOKU_TMP_DATA . 'cache/debug.log');
75        }
76    }
77
78    public function testIndex(): void {
79        $query = array('ross');
80        $this->assertEquals(
81            array(
82                'ross' => array(
83                    'link'                     => '3',
84                    'bob_ross_says'            => '1',
85                    'backlinks_syntax'         => '2',
86                    'backlinks_include_syntax' => '2',
87                    'backlinks_exclude_syntax' => '2',
88                    'backlink_test_pages'      => '8',
89                    'include:link'             => '3',
90                    'exclude:link'             => '3'
91                )
92            ),
93            idx_lookup($query)
94        );
95    }
96
97    public function testLinksPage(): void {
98        $request  = new TestRequest();
99        $response = $request->get(array('id' => 'link'), '/doku.php');
100
101        $this->assertTrue(
102            strpos($response->getContent(), 'A link to Bob Ross') !== false,
103            '"A link to Bob Ross" was not in the output'
104        );
105    }
106
107    public function testStoryPage(): void {
108        $request  = new TestRequest();
109        $response = $request->get(array('id' => 'bob_ross_says'), '/doku.php');
110
111        $this->assertTrue(
112            strpos($response->getContent(), 'Bob Ross says') !== false,
113            '"Bob Ross says" was not in the output'
114        );
115    }
116
117    public function testBacklinks(): void {
118        $request  = new TestRequest();
119        $response = $request->get(array('id' => 'backlinks_syntax'), '/doku.php');
120
121        $this->assertTrue(
122            strpos($response->getContent(), 'Backlinks to what Bob Ross says') !== false,
123            '"Backlinks to what Bob Ross says" was not in the output'
124        );
125
126        $doc = phpQuery::newDocument($response->getContent());
127        // look for id="plugin__backlinks"
128        $this->assertEquals(
129            1,
130            pq('#plugin__backlinks', $doc)->length,
131            'There should be one backlinks element'
132        );
133
134        $wikilinks = pq('#plugin__backlinks ul li', $doc);
135        dbglog($wikilinks->text(), 'found backlinks');
136        $this->assertEquals(
137            4,
138            $wikilinks->contents()->length,
139            'There should be 4 backlinks'
140        );
141
142        $lastlink = pq('a:last', $wikilinks);
143        dbglog($lastlink->text(), "last backlink");
144        $this->assertEquals(
145            $lastlink->text(),
146            'A link to Bob Ross',
147            'The last backlink should be a link to Bob Ross'
148        );
149    }
150}
151