File: IndexesTest.php

package info (click to toggle)
phpmyadmin 4%3A5.2.2-really%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 140,348 kB
  • sloc: javascript: 228,478; php: 166,896; xml: 17,847; sql: 504; sh: 275; makefile: 209; python: 205
file content (107 lines) | stat: -rw-r--r-- 3,274 bytes parent folder | download | duplicates (3)
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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Table;

use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Index;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Table;
use PhpMyAdmin\Table\Indexes;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer as ResponseStub;

/**
 * @covers \PhpMyAdmin\Table\Indexes
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Table\Indexes::class)]
class IndexesTest extends AbstractTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        parent::setTheme();

        /**
         * SET these to avoid undefined index error
         */
        $GLOBALS['server'] = 1;
        $GLOBALS['db'] = 'db';
        $GLOBALS['table'] = 'table';
        $GLOBALS['text_dir'] = 'ltr';
        $GLOBALS['PMA_PHP_SELF'] = 'index.php';
        $GLOBALS['cfg']['Server']['pmadb'] = '';
        $GLOBALS['cfg']['Server']['DisableIS'] = false;
        $GLOBALS['urlParams'] = [
            'db' => 'db',
            'server' => 1,
        ];

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

        $indexs = [
            [
                'Schema' => 'Schema1',
                'Key_name' => 'Key_name1',
                'Column_name' => 'Column_name1',
            ],
            [
                'Schema' => 'Schema2',
                'Key_name' => 'Key_name2',
                'Column_name' => 'Column_name2',
            ],
            [
                'Schema' => 'Schema3',
                'Key_name' => 'Key_name3',
                'Column_name' => 'Column_name3',
            ],
        ];

        $dbi->expects($this->any())->method('getTableIndexes')
            ->willReturn($indexs);

        $GLOBALS['dbi'] = $dbi;

        //$_SESSION
    }

    public function testDoSaveData(): void
    {
        $sql_query = 'ALTER TABLE `db`.`table` DROP PRIMARY KEY, ADD UNIQUE ;';

        $table = $this->getMockBuilder(Table::class)
            ->disableOriginalConstructor()
            ->getMock();
        $table->expects($this->any())->method('getSqlQueryForIndexCreateOrEdit')
            ->willReturn($sql_query);

        $GLOBALS['dbi']->expects($this->any())->method('getTable')
            ->willReturn($table);

        $response = new ResponseStub();
        $index = new Index();

        $indexes = new Indexes($response, new Template(), $GLOBALS['dbi']);

        // Preview SQL
        $_POST['preview_sql'] = true;
        $indexes->doSaveData($index, false, $GLOBALS['db'], $GLOBALS['table']);
        $jsonArray = $response->getJSONResult();
        self::assertArrayHasKey('sql_data', $jsonArray);
        self::assertStringContainsString($sql_query, $jsonArray['sql_data']);

        // Alter success
        $response->clear();
        ResponseRenderer::getInstance()->setAjax(true);
        unset($_POST['preview_sql']);
        $indexes->doSaveData($index, false, $GLOBALS['db'], $GLOBALS['table']);
        $jsonArray = $response->getJSONResult();
        self::assertArrayHasKey('index_table', $jsonArray);
        self::assertArrayHasKey('message', $jsonArray);
        ResponseRenderer::getInstance()->setAjax(false);
    }
}