xref: /plugin/dw2pdf/_test/BookCreatorLiveSelectionCollectorTest.php (revision 92200750cd9cfc4a9489fe7c04c6e41b5a3de6ee)
1<?php
2
3namespace dokuwiki\plugin\dw2pdf\test;
4
5use dokuwiki\plugin\dw2pdf\src\BookCreatorLiveSelectionCollector;
6use dokuwiki\plugin\dw2pdf\src\Config;
7use DokuWikiTest;
8
9/**
10 * @group plugin_dw2pdf
11 * @group plugins
12 */
13class BookCreatorLiveSelectionCollectorTest extends DokuWikiTest
14{
15    public function setUp(): void
16    {
17        parent::setUp();
18        $_REQUEST = [];
19    }
20
21    /**
22     * Live selections should be decoded and normalized via cleanID().
23     */
24    public function testCollectsPagesFromJsonSelection(): void
25    {
26        global $INPUT;
27        $pageA = 'playground:bookcreator:a';
28        $pageB = 'playground:bookcreator:Child';
29        $pageC = 'playground:bookcreator:nonexistent';
30        saveWikiText($pageA, 'A', 'create');
31        saveWikiText($pageB, 'B', 'create');
32
33        // page B has mixed case to test cleanID()
34        $INPUT->set('selection', json_encode([$pageA, $pageB, $pageC])); // page B has mixed case to test cleanID()
35
36        $collector = new BookCreatorLiveSelectionCollector(new Config());
37        $this->assertSame(
38            [$pageA, 'playground:bookcreator:child'], // page C should be skipped as it does not exist
39            $collector->getPages()
40        );
41    }
42
43    /**
44     * Invalid JSON payloads must bubble up as JsonException so the caller can handle them.
45     */
46    public function testThrowsOnInvalidJson(): void
47    {
48        global $INPUT;
49        $INPUT->set('selection', '{invalid');
50
51        $this->expectException(\JsonException::class);
52        new BookCreatorLiveSelectionCollector(new Config());
53    }
54}
55