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