File: DesignerTest.php

package info (click to toggle)
phpmyadmin 4%3A5.2.1%2Bdfsg-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 131,332 kB
  • sloc: javascript: 212,681; php: 168,094; xml: 18,098; sql: 504; sh: 274; makefile: 205; python: 199
file content (204 lines) | stat: -rw-r--r-- 6,886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Database;

use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Database\Designer;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\DummyResult;
use PhpMyAdmin\Version;
use ReflectionMethod;

/**
 * @covers \PhpMyAdmin\Database\Designer
 */
class DesignerTest extends AbstractTestCase
{
    /** @var Designer */
    private $designer;

    /**
     * Setup for test cases
     */
    protected function setUp(): void
    {
        parent::setUp();

        $GLOBALS['server'] = 1;
        $GLOBALS['cfg']['ServerDefault'] = 1;
        $GLOBALS['cfg']['PDFPageSizes'] = [
            'A3',
            'A4',
        ];
        $GLOBALS['cfg']['PDFDefaultPageSize'] = 'A4';
        $GLOBALS['cfg']['Schema']['pdf_orientation'] = 'L';
        $GLOBALS['cfg']['Schema']['pdf_paper'] = 'A4';

        $_SESSION = [
            'relation' => [
                1 => [
                    'version' => Version::VERSION,
                    'db' => 'pmadb',
                    'pdf_pages' => 'pdf_pages',
                    'table_coords' => 'table_coords',
                    'pdfwork' => true,
                ],
            ],
            ' PMA_token ' => 'token',
        ];
    }

    /**
     * Mocks database interaction for tests.
     *
     * @param string $db database name
     */
    private function mockDatabaseInteraction(string $db): void
    {
        $resultStub = $this->createMock(DummyResult::class);

        $dbi = $this->getMockBuilder(DatabaseInterface::class)
            ->disableOriginalConstructor()
            ->getMock();

        $dbi->expects($this->once())
            ->method('tryQueryAsControlUser')
            ->with(
                'SELECT `page_nr`, `page_descr` FROM `pmadb`.`pdf_pages`'
                . " WHERE db_name = '" . $db . "' ORDER BY `page_descr`"
            )
            ->will($this->returnValue($resultStub));

        $resultStub->expects($this->exactly(3))
            ->method('fetchAssoc')
            ->willReturnOnConsecutiveCalls(
                [
                    'page_nr' => '1',
                    'page_descr' => 'page1',
                ],
                [
                    'page_nr' => '2',
                    'page_descr' => 'page2',
                ],
                []
            );

        $dbi->expects($this->any())
            ->method('escapeString')
            ->will($this->returnArgument(0));

        $GLOBALS['dbi'] = $dbi;
    }

    /**
     * Test for getPageIdsAndNames()
     */
    public function testGetPageIdsAndNames(): void
    {
        $db = 'db';
        $this->mockDatabaseInteraction($db);

        $this->designer = new Designer($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());

        $method = new ReflectionMethod(Designer::class, 'getPageIdsAndNames');
        $method->setAccessible(true);
        $result = $method->invokeArgs($this->designer, [$db]);

        $this->assertEquals(
            [
                '1' => 'page1',
                '2' => 'page2',
            ],
            $result
        );
    }

    /**
     * Test for getHtmlForEditOrDeletePages()
     */
    public function testGetHtmlForEditOrDeletePages(): void
    {
        $db = 'db';
        $operation = 'edit';
        $this->mockDatabaseInteraction($db);

        $this->designer = new Designer($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());

        $result = $this->designer->getHtmlForEditOrDeletePages($db, $operation);
        $this->assertStringContainsString('<input type="hidden" name="operation" value="' . $operation . '">', $result);
        $this->assertStringContainsString('<select name="selected_page" id="selected_page">', $result);
        $this->assertStringContainsString('<option value="0">', $result);
        $this->assertStringContainsString('<option value="1">', $result);
        $this->assertStringContainsString('page1', $result);
        $this->assertStringContainsString('<option value="2">', $result);
        $this->assertStringContainsString('page2', $result);
    }

    /**
     * Test for getHtmlForPageSaveAs()
     */
    public function testGetHtmlForPageSaveAs(): void
    {
        $db = 'db';
        $this->mockDatabaseInteraction($db);

        $this->designer = new Designer($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());

        $result = $this->designer->getHtmlForPageSaveAs($db);
        $this->assertStringContainsString('<input type="hidden" name="operation" value="savePage">', $result);
        $this->assertStringContainsString('<select name="selected_page" id="selected_page">', $result);
        $this->assertStringContainsString('<option value="0">', $result);
        $this->assertStringContainsString('<option value="1">', $result);
        $this->assertStringContainsString('page1', $result);
        $this->assertStringContainsString('<option value="2">', $result);
        $this->assertStringContainsString('page2', $result);

        $this->assertStringContainsString(
            '<input type="radio" name="save_page" id="savePageSameRadio" value="same" checked>',
            $result
        );
        $this->assertStringContainsString(
            '<input type="radio" name="save_page" id="savePageNewRadio" value="new">',
            $result
        );
        $this->assertStringContainsString('<input type="text" name="selected_value" id="selected_value">', $result);
    }

    /**
     * Test for getHtmlForSchemaExport()
     */
    public function testGetHtmlForSchemaExport(): void
    {
        $db = 'db';
        $page = 2;

        $this->designer = new Designer($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());

        $result = $this->designer->getHtmlForSchemaExport($db, $page);
        // export type
        $this->assertStringContainsString('<select id="plugins" name="export_type">', $result);

        // hidden field
        $this->assertStringContainsString('<input type="hidden" name="page_number" value="' . $page . '">', $result);

        // orientation
        $this->assertStringContainsString(
            '<select class="form-select" name="pdf_orientation" id="select_pdf_orientation">',
            $result
        );
        $this->assertStringContainsString('<option value="L" selected>Landscape</option>', $result);
        $this->assertStringContainsString('<option value="P">Portrait</option>', $result);

        // paper size
        $this->assertStringContainsString(
            '<select class="form-select" name="pdf_paper" id="select_pdf_paper">',
            $result
        );
        $this->assertStringContainsString('<option value="A3">A3</option>', $result);
        $this->assertStringContainsString('<option value="A4" selected>A4</option>', $result);
    }
}