xref: /dokuwiki/_test/tests/Search/Query/NamespacePredicateTest.php (revision ede4646658cf51245060332d97a319a39c788ea1)
1<?php
2
3namespace dokuwiki\test\Search\Query;
4
5use dokuwiki\Search\Query\NamespacePredicate;
6use dokuwiki\Search\Query\PageSet;
7
8class NamespacePredicateTest extends \DokuWikiTest
9{
10    protected PageSet $pages;
11
12    public function setUp(): void
13    {
14        parent::setUp();
15        $this->pages = new PageSet([
16            'wiki:start' => 5,
17            'wiki:syntax' => 3,
18            'wiki:sub:page' => 2,
19            'other:page' => 4,
20            'other:deep:nested' => 1,
21            'toplevel' => 6,
22        ]);
23    }
24
25    public function testGetPrefix()
26    {
27        $ns = new NamespacePredicate('wiki:');
28        $this->assertEquals('wiki:', $ns->getPrefix());
29    }
30
31    public function testFilterKeepsMatchingNamespace()
32    {
33        $ns = new NamespacePredicate('wiki:');
34        $result = $ns->filter($this->pages);
35
36        $this->assertEquals([
37            'wiki:start' => 5,
38            'wiki:syntax' => 3,
39            'wiki:sub:page' => 2,
40        ], $result->getPages());
41    }
42
43    public function testFilterIncludesSubNamespaces()
44    {
45        $ns = new NamespacePredicate('wiki:');
46        $result = $ns->filter($this->pages);
47
48        $this->assertArrayHasKey('wiki:sub:page', $result->getPages());
49    }
50
51    public function testFilterDoesNotMatchPartialPrefix()
52    {
53        // "other:" must not match "otherstuff:page"
54        $pages = new PageSet([
55            'other:page' => 1,
56            'otherstuff:page' => 2,
57        ]);
58        $ns = new NamespacePredicate('other:');
59        $result = $ns->filter($pages);
60
61        $this->assertEquals(['other:page' => 1], $result->getPages());
62    }
63
64    public function testExcludeRemovesMatchingNamespace()
65    {
66        $ns = new NamespacePredicate('wiki:');
67        $result = $ns->exclude($this->pages);
68
69        $this->assertEquals([
70            'other:page' => 4,
71            'other:deep:nested' => 1,
72            'toplevel' => 6,
73        ], $result->getPages());
74    }
75
76    public function testFilterOnEmptyPageSet()
77    {
78        $ns = new NamespacePredicate('wiki:');
79        $result = $ns->filter(new PageSet());
80
81        $this->assertEquals([], $result->getPages());
82    }
83
84    public function testFilterNoMatch()
85    {
86        $ns = new NamespacePredicate('nonexistent:');
87        $result = $ns->filter($this->pages);
88
89        $this->assertEquals([], $result->getPages());
90    }
91
92    public function testExcludeNoMatch()
93    {
94        $ns = new NamespacePredicate('nonexistent:');
95        $result = $ns->exclude($this->pages);
96
97        // all pages should remain
98        $this->assertEquals($this->pages->getPages(), $result->getPages());
99    }
100
101    public function testPreservesScores()
102    {
103        $ns = new NamespacePredicate('other:');
104        $result = $ns->filter($this->pages);
105
106        $this->assertEquals(4, $result->getPages()['other:page']);
107        $this->assertEquals(1, $result->getPages()['other:deep:nested']);
108    }
109}
110