3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests\Controllers\Table
;
7 use PhpMyAdmin\Controllers\Table\RelationController
;
8 use PhpMyAdmin\DatabaseInterface
;
9 use PhpMyAdmin\Relation
;
11 use PhpMyAdmin\Template
;
12 use PhpMyAdmin\Tests\AbstractTestCase
;
13 use PhpMyAdmin\Tests\Stubs\Response
as ResponseStub
;
16 class RelationControllerTest
extends AbstractTestCase
18 /** @var ResponseStub */
25 * Configures environment
27 protected function setUp(): void
30 parent
::defineVersionConstants();
31 parent
::loadDefaultConfig();
34 $GLOBALS['server'] = 0;
35 $GLOBALS['db'] = 'db';
36 $GLOBALS['table'] = 'table';
37 $GLOBALS['text_dir'] = 'ltr';
38 $GLOBALS['PMA_PHP_SELF'] = 'index.php';
39 $GLOBALS['cfg']['Server']['DisableIS'] = false;
42 $_POST['foreignDb'] = 'db';
43 $_POST['foreignTable'] = 'table';
45 $GLOBALS['dblist'] = new stdClass();
46 $GLOBALS['dblist']->databases
= new class
49 * @param mixed $name name
51 public function exists($name): bool
57 $dbi = $this->getMockBuilder(DatabaseInterface
::class)
58 ->disableOriginalConstructor()
61 $GLOBALS['dbi'] = $dbi;
63 $this->response
= new ResponseStub();
64 $this->template
= new Template();
68 * Tests for getDropdownValueForTableAction()
70 * Case one: this case is for the situation when the target
75 public function testGetDropdownValueForTableActionIsView(): void
82 $tableMock = $this->getMockBuilder(Table
::class)
83 ->disableOriginalConstructor()
85 // Test the situation when the table is a view
86 $tableMock->expects($this->any())->method('isView')
87 ->will($this->returnValue(true));
88 $tableMock->expects($this->any())->method('getColumns')
89 ->will($this->returnValue($viewColumns));
91 $GLOBALS['dbi']->expects($this->any())->method('getTable')
92 ->will($this->returnValue($tableMock));
94 $ctrl = new RelationController(
99 new Relation($GLOBALS['dbi'], $this->template
),
103 $ctrl->getDropdownValueForTable();
104 $json = $this->response
->getJSONResult();
112 * Tests for getDropdownValueForTableAction()
114 * Case one: this case is for the situation when the target
115 * table is not a view (real tabletable).
119 public function testGetDropdownValueForTableActionNotView(): void
121 $indexedColumns = ['primaryTableCol'];
122 $tableMock = $this->getMockBuilder(Table
::class)
123 ->disableOriginalConstructor()
125 // Test the situation when the table is a view
126 $tableMock->expects($this->any())->method('isView')
127 ->will($this->returnValue(false));
128 $tableMock->expects($this->any())->method('getIndexedColumns')
129 ->will($this->returnValue($indexedColumns));
131 $GLOBALS['dbi']->expects($this->any())->method('getTable')
132 ->will($this->returnValue($tableMock));
134 $ctrl = new RelationController(
139 new Relation($GLOBALS['dbi'], $this->template
),
143 $ctrl->getDropdownValueForTable();
144 $json = $this->response
->getJSONResult();
152 * Tests for getDropdownValueForDbAction()
158 public function testGetDropdownValueForDbActionOne(): void
160 $GLOBALS['dbi']->expects($this->any())
161 ->method('fetchArray')
163 $this->returnCallback(
170 'Engine' => 'InnoDB',
180 $ctrl = new RelationController(
185 new Relation($GLOBALS['dbi'], $this->template
),
189 $_POST['foreign'] = 'true';
190 $ctrl->getDropdownValueForDatabase('INNODB');
191 $json = $this->response
->getJSONResult();
199 * Tests for getDropdownValueForDbAction()
201 * Case two: not foreign
205 public function testGetDropdownValueForDbActionTwo(): void
207 $GLOBALS['dbi']->expects($this->any())
208 ->method('fetchArray')
210 $this->returnCallback(
224 $ctrl = new RelationController(
229 new Relation($GLOBALS['dbi'], $this->template
),
233 $_POST['foreign'] = 'false';
234 $ctrl->getDropdownValueForDatabase('INNODB');
235 $json = $this->response
->getJSONResult();