1<?php
2
3namespace dokuwiki\plugin\structautolink\test;
4
5use DokuWikiTest;
6
7/**
8 * FIXME tests for the structautolink plugin
9 *
10 * @group plugin_structautolink
11 * @group plugins
12 */
13class RendererTest extends DokuWikiTest
14{
15    public function testFindMatchingTokens()
16    {
17        $R = new \renderer_plugin_structautolink();
18        $R->setGlossary([
19            'ml' => ['ML', 'Maschinelles Lernen', 'Machine Learning'],
20            'ki' => ['KI', 'Künstliche Intelligenz', 'AI', 'Artificial Intelligence'],
21            'dl' => ['DL', 'Deep Learning'],
22            'nlp' => ['NLP', 'Natural Language Processing'],
23        ]);
24
25        $text = 'Was wir über Künstliche Intelligenz wissen, kann uns nur Machine Learning beantworten. dl ist egal.';
26        $result = $R->findMatchingTokens($text);
27
28        $this->assertEquals(
29            [
30                [
31                    'id' => 'ki',
32                    'term' => 'Künstliche Intelligenz',
33                    'pos' => 14,
34                    'len' => 23,
35                ],
36                [
37                    'id' => 'ml',
38                    'term' => 'Machine Learning',
39                    'pos' => 59,
40                    'len' => 16,
41                ],
42            ],
43            $result
44        );
45
46        // check that positions above are actually correct
47        $this->assertEquals($result[0]['term'], substr($text, $result[0]['pos'], $result[0]['len']));
48        $this->assertEquals($result[1]['term'], substr($text, $result[1]['pos'], $result[1]['len']));
49    }
50
51    public function testCdata()
52    {
53        $R = new \renderer_plugin_structautolink();
54        $R->setGlossary([
55            'ml' => ['ML', 'Maschinelles Lernen', 'Machine Learning'],
56            'ki' => ['KI', 'Künstliche Intelligenz', 'AI', 'Artificial Intelligence'],
57            'dl' => ['DL', 'Deep Learning'],
58            'nlp' => ['NLP', 'Natural Language Processing'],
59        ]);
60
61        $R->cdata('Was wir über Künstliche Intelligenz wissen, kann uns nur Machine Learning beantworten.');
62        $R->cdata('Künstliche Intelligenz ist KI'); // should not be linked again
63        $R->cdata('dl ist egal.'); // lowercase != uppercase
64        $result = $R->doc;
65
66        $this->assertStringStartsWith('Was wir über <a href', $result);
67        $this->assertStringEndsWith('dl ist egal.', $result);
68
69        if(class_exists('DOMWrap\Document')) {
70            $pq = (new \DOMWrap\Document())->html($result);
71        } else {
72            // deprecated
73            $pq = \phpQuery::newDocumentHTML($result);
74        }
75
76        $this->assertEquals(2, $pq->find('a')->count());
77        $this->assertEquals('Künstliche Intelligenz', $pq->find('a')->eq(0)->text());
78        $this->assertEquals('Machine Learning', $pq->find('a')->eq(1)->text());
79    }
80}
81