xref: /plugin/dw2pdf/_test/NamespaceCollectorSortTest.php (revision 92200750cd9cfc4a9489fe7c04c6e41b5a3de6ee)
1<?php
2
3namespace dokuwiki\plugin\dw2pdf\test;
4
5use dokuwiki\plugin\dw2pdf\src\NamespaceCollector;
6use DokuWikiTest;
7use ReflectionClass;
8
9/**
10 * @group plugin_dw2pdf
11 * @group plugins
12 */
13class NamespaceCollectorSortTest extends DokuWikiTest
14{
15    /**
16     * Provide a list of page orderings that should remain stable after sorting.
17     *
18     * @see testPagenameSort
19     * @return array
20     */
21    public function providerPageNameSort(): array
22    {
23        return [
24            'start pages sorted' => [[
25                'bar',
26                'bar:start',
27                'bar:alpha',
28                'bar:bar',
29            ]],
30
31            'pages and subspaces mixed' => [[
32                'alpha',
33                'beta:foo',
34                'gamma',
35            ]],
36
37            'full test' => [[
38                'start',
39                '01_page',
40                '10_page',
41                'bar',
42                'bar:start',
43                'bar:1_page',
44                'bar:2_page',
45                'bar:10_page',
46                'bar:22_page',
47                'bar:aa_page',
48                'bar:aa_page:detail1',
49                'bar:zz_page',
50                'foo',
51                'foo:start',
52                'foo:01_page',
53                'foo:10_page',
54                'foo:foo',
55                'foo:zz_page',
56                'ns',
57                'ns:01_page',
58                'ns:10_page',
59                'ns:ns',
60                'ns:zz_page',
61                'zz_page',
62            ]],
63        ];
64    }
65
66    /**
67     * Ensure natural name sorting remains stable for multiple namespace scenarios.
68     *
69     * @dataProvider providerPageNameSort
70     * @param array $expected
71     */
72    public function testPagenameSort(array $expected): void
73    {
74        // Build a namespace collector instance without running the heavy constructor logic.
75        $reflection = new ReflectionClass(NamespaceCollector::class);
76        /** @var NamespaceCollector $collector */
77        $collector = $reflection->newInstanceWithoutConstructor();
78
79        $prepared = [];
80        foreach ($expected as $line) {
81            $prepared[] = ['id' => $line];
82        }
83
84        $input = $prepared;
85        shuffle($input);
86
87        usort($input, [$collector, 'cbPagenameSort']);
88
89        $this->assertSame($prepared, $input);
90    }
91}
92