1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\Search;
6
7/**
8 * Testing the CSV exports of aggregations
9 *
10 * @group plugin_struct
11 * @group plugins
12 */
13class ImportPageCSV extends StructTest
14{
15
16    public function setUp(): void
17    {
18        parent::setUp();
19
20        $this->loadSchemaJSON('schema1');
21    }
22
23    public function test_importExistingPageCSV()
24    {
25        $csvImporter = new mock\CSVPageImporter('schema1', '', 'page');
26        $csvImporter->setTestData([
27            ['pid', 'first', 'second', 'third', 'fourth'],
28            ['wiki:syntax', 'e', 'f,i', 'g', 'h'],
29        ]);
30        $assignment = mock\Assignments::getInstance();
31        $assignment->addPattern('wiki:syntax', 'schema1');
32        $csvImporter->import();
33
34        $schemaData = mock\AccessTable::getPageAccess('schema1', 'wiki:syntax');
35        $actual_data = $schemaData->getDataFromDB();
36
37        $expected_data = array(
38            array(
39                'PID' => 'wiki:syntax',
40                'out1' => 'e',
41                'out2' => 'f' . Search::CONCAT_SEPARATOR . 'i',
42                'out3' => 'g',
43                'out4' => 'h',
44            ),
45        );
46
47        $this->assertSame($expected_data, $actual_data);
48    }
49
50    /**
51     * Unknown header should be discarded/ignored
52     */
53    public function test_importNewPageCSV()
54    {
55        // arrange
56        global $INPUT;
57        $INPUT->set('createPage', true);
58        $pageID = 'new:page';
59        $csvImporter = new mock\CSVPageImporter('schema1', '', 'page');
60        $csvImporter->setTestData([
61            ['pid', 'first', 'third', 'second', 'fourth', 'fifth'],
62            [$pageID, 'a', 'c', 'b,e', 'd',],
63        ]);
64        $templateFN = substr(wikiFN($pageID), 0, -1 * strlen('page.txt')) . '_template.txt';
65        io_makeFileDir($templateFN);
66        file_put_contents($templateFN,
67            '====== @PAGE@ ======
68<ifnotempty schema1.first>
69first: @@schema1.first@@
70</ifnotempty>
71second: ##schema1.second##
72<ifnotempty fifth>
73fifth: @@fifth@@
74</ifnotempty>');
75
76        // act
77        $assignment = mock\Assignments::getInstance();
78        $assignment->addPattern($pageID, 'schema1');
79        $csvImporter->import();
80
81        // assert
82        $schemaData = mock\AccessTable::getPageAccess('schema1', $pageID);
83        $actual_data = $schemaData->getDataFromDB();
84        $expected_data = array(
85            array(
86                'PID' => $pageID,
87                'out1' => 'a',
88                'out2' => 'b' . Search::CONCAT_SEPARATOR . 'e',
89                'out3' => 'c',
90                'out4' => 'd',
91            ),
92        );
93        $this->assertSame($expected_data, $actual_data);
94        $this->assertTrue(page_exists($pageID));
95        $expectedText = '====== page ======
96
97first: a
98
99second: b, e
100';
101        $text = file_get_contents(wikiFN($pageID));
102        $this->assertSame($expectedText, $text);
103    }
104}
105