xref: /plugin/dw2pdf/_test/CollectorFactoryTest.php (revision 4c5691130dbf9035b960f10f5b3cb4b319a026d8)
192200750SAndreas Gohr<?php
292200750SAndreas Gohr
392200750SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf\test;
492200750SAndreas Gohr
592200750SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\BookCreatorLiveSelectionCollector;
692200750SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\BookCreatorSavedSelectionCollector;
792200750SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\CollectorFactory;
892200750SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\Config;
9*4c569113SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\ExportException;
1092200750SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\NamespaceCollector;
1192200750SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\PageCollector;
1292200750SAndreas Gohruse DokuWikiTest;
1392200750SAndreas Gohr
1492200750SAndreas Gohr/**
1592200750SAndreas Gohr * @group plugin_dw2pdf
1692200750SAndreas Gohr * @group plugins
1792200750SAndreas Gohr */
1892200750SAndreas Gohrclass CollectorFactoryTest extends DokuWikiTest
1992200750SAndreas Gohr{
2092200750SAndreas Gohr    public function setUp(): void
2192200750SAndreas Gohr    {
2292200750SAndreas Gohr        parent::setUp();
2392200750SAndreas Gohr        $_REQUEST = [];
2492200750SAndreas Gohr    }
2592200750SAndreas Gohr
2692200750SAndreas Gohr    /**
2792200750SAndreas Gohr     * Factory should return the single-page collector for simple exports.
2892200750SAndreas Gohr     */
2992200750SAndreas Gohr    public function testCreatesPageCollector(): void
3092200750SAndreas Gohr    {
31*4c569113SAndreas Gohr        global $ID, $INPUT;
32*4c569113SAndreas Gohr        $ID = 'playground:dw2pdffactory';
33*4c569113SAndreas Gohr        $INPUT->set('id', $ID);
34*4c569113SAndreas Gohr        saveWikiText($ID, 'factory test content', 'create test page');
35*4c569113SAndreas Gohr
3692200750SAndreas Gohr        $config = new Config();
3792200750SAndreas Gohr        $collector = CollectorFactory::create('export_pdf', $config, null, null);
3892200750SAndreas Gohr        $this->assertInstanceOf(PageCollector::class, $collector);
3992200750SAndreas Gohr    }
4092200750SAndreas Gohr
4192200750SAndreas Gohr    /**
4292200750SAndreas Gohr     * Factory should switch to namespace collector when export_pdfns is requested.
4392200750SAndreas Gohr     */
4492200750SAndreas Gohr    public function testCreatesNamespaceCollector(): void
4592200750SAndreas Gohr    {
4692200750SAndreas Gohr        global $INPUT;
4792200750SAndreas Gohr        $INPUT->set('book_ns', 'wiki');
4892200750SAndreas Gohr        $config = new Config();
4992200750SAndreas Gohr        $collector = CollectorFactory::create('export_pdfns', $config, null, null);
5092200750SAndreas Gohr        $this->assertInstanceOf(NamespaceCollector::class, $collector);
5192200750SAndreas Gohr    }
5292200750SAndreas Gohr
5392200750SAndreas Gohr    /**
5492200750SAndreas Gohr     * When a live selection payload is present, the live collector is used.
5592200750SAndreas Gohr     */
5692200750SAndreas Gohr    public function testCreatesLiveSelectionCollector(): void
5792200750SAndreas Gohr    {
5892200750SAndreas Gohr        global $INPUT;
5992200750SAndreas Gohr        $INPUT->set('selection', '["wiki:start"]');
6092200750SAndreas Gohr        $config = new Config();
6192200750SAndreas Gohr        $collector = CollectorFactory::create('export_pdfbook', $config, null, null);
6292200750SAndreas Gohr        $this->assertInstanceOf(BookCreatorLiveSelectionCollector::class, $collector);
6392200750SAndreas Gohr    }
6492200750SAndreas Gohr
6592200750SAndreas Gohr    /**
6692200750SAndreas Gohr     * When only a saved selection identifier is present, use the saved collector.
6792200750SAndreas Gohr     */
6892200750SAndreas Gohr    public function testCreatesSavedSelectionCollector(): void
6992200750SAndreas Gohr    {
7092200750SAndreas Gohr        global $INPUT;
7192200750SAndreas Gohr        $INPUT->set('savedselection', 'my-saved-selection');
7292200750SAndreas Gohr        $config = new Config();
7392200750SAndreas Gohr        $collector = CollectorFactory::create('export_pdfbook', $config, null, null);
7492200750SAndreas Gohr        $this->assertInstanceOf(BookCreatorSavedSelectionCollector::class, $collector);
7592200750SAndreas Gohr    }
7692200750SAndreas Gohr
7792200750SAndreas Gohr    /**
7892200750SAndreas Gohr     * A pdfbook export without any selection should be rejected.
7992200750SAndreas Gohr     */
8092200750SAndreas Gohr    public function testBadSelectionCollector(): void
8192200750SAndreas Gohr    {
82*4c569113SAndreas Gohr        $this->expectException(ExportException::class);
83*4c569113SAndreas Gohr        $this->expectExceptionMessage('empty');
8492200750SAndreas Gohr        $config = new Config();
8592200750SAndreas Gohr        CollectorFactory::create('export_pdfbook', $config, null, null);
8692200750SAndreas Gohr    }
8792200750SAndreas Gohr
8892200750SAndreas Gohr    /**
8992200750SAndreas Gohr     * Unknown export actions should be rejected early.
9092200750SAndreas Gohr     */
9192200750SAndreas Gohr    public function testRejectsUnknownEvent(): void
9292200750SAndreas Gohr    {
9392200750SAndreas Gohr        $this->expectException(\InvalidArgumentException::class);
9492200750SAndreas Gohr        $config = new Config();
9592200750SAndreas Gohr        CollectorFactory::create('random', $config, null, null);
9692200750SAndreas Gohr    }
9792200750SAndreas Gohr}
98