Remove some useless comments
[phpmyadmin.git] / test / classes / Controllers / Table / RelationControllerTest.php
blob2e22176e75ebc7000224cc37e4417a1344d56d42
1 <?php
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;
10 use PhpMyAdmin\Table;
11 use PhpMyAdmin\Template;
12 use PhpMyAdmin\Tests\AbstractTestCase;
13 use PhpMyAdmin\Tests\Stubs\Response as ResponseStub;
14 use stdClass;
16 class RelationControllerTest extends AbstractTestCase
18 /** @var ResponseStub */
19 private $response;
21 /** @var Template */
22 private $template;
24 /**
25 * Configures environment
27 protected function setUp(): void
29 parent::setUp();
30 parent::defineVersionConstants();
31 parent::loadDefaultConfig();
32 parent::setTheme();
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;
40 //$_SESSION
42 $_POST['foreignDb'] = 'db';
43 $_POST['foreignTable'] = 'table';
45 $GLOBALS['dblist'] = new stdClass();
46 $GLOBALS['dblist']->databases = new class
48 /**
49 * @param mixed $name name
51 public function exists($name): bool
53 return true;
57 $dbi = $this->getMockBuilder(DatabaseInterface::class)
58 ->disableOriginalConstructor()
59 ->getMock();
61 $GLOBALS['dbi'] = $dbi;
63 $this->response = new ResponseStub();
64 $this->template = new Template();
67 /**
68 * Tests for getDropdownValueForTableAction()
70 * Case one: this case is for the situation when the target
71 * table is a view.
73 * @test
75 public function testGetDropdownValueForTableActionIsView(): void
77 $viewColumns = [
78 'viewCol',
79 'viewCol2',
80 'viewCol3',
82 $tableMock = $this->getMockBuilder(Table::class)
83 ->disableOriginalConstructor()
84 ->getMock();
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(
95 $this->response,
96 $this->template,
97 $GLOBALS['db'],
98 $GLOBALS['table'],
99 new Relation($GLOBALS['dbi'], $this->template),
100 $GLOBALS['dbi']
103 $ctrl->getDropdownValueForTable();
104 $json = $this->response->getJSONResult();
105 $this->assertEquals(
106 $viewColumns,
107 $json['columns']
112 * Tests for getDropdownValueForTableAction()
114 * Case one: this case is for the situation when the target
115 * table is not a view (real tabletable).
117 * @test
119 public function testGetDropdownValueForTableActionNotView(): void
121 $indexedColumns = ['primaryTableCol'];
122 $tableMock = $this->getMockBuilder(Table::class)
123 ->disableOriginalConstructor()
124 ->getMock();
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(
135 $this->response,
136 $this->template,
137 $GLOBALS['db'],
138 $GLOBALS['table'],
139 new Relation($GLOBALS['dbi'], $this->template),
140 $GLOBALS['dbi']
143 $ctrl->getDropdownValueForTable();
144 $json = $this->response->getJSONResult();
145 $this->assertEquals(
146 $indexedColumns,
147 $json['columns']
152 * Tests for getDropdownValueForDbAction()
154 * Case one: foreign
156 * @test
158 public function testGetDropdownValueForDbActionOne(): void
160 $GLOBALS['dbi']->expects($this->any())
161 ->method('fetchArray')
162 ->will(
163 $this->returnCallback(
164 static function () {
165 static $count = 0;
166 if ($count == 0) {
167 $count++;
169 return [
170 'Engine' => 'InnoDB',
171 'Name' => 'table',
175 return null;
180 $ctrl = new RelationController(
181 $this->response,
182 $this->template,
183 $GLOBALS['db'],
184 $GLOBALS['table'],
185 new Relation($GLOBALS['dbi'], $this->template),
186 $GLOBALS['dbi']
189 $_POST['foreign'] = 'true';
190 $ctrl->getDropdownValueForDatabase('INNODB');
191 $json = $this->response->getJSONResult();
192 $this->assertEquals(
193 ['table'],
194 $json['tables']
199 * Tests for getDropdownValueForDbAction()
201 * Case two: not foreign
203 * @test
205 public function testGetDropdownValueForDbActionTwo(): void
207 $GLOBALS['dbi']->expects($this->any())
208 ->method('fetchArray')
209 ->will(
210 $this->returnCallback(
211 static function () {
212 static $count = 0;
213 if ($count == 0) {
214 $count++;
216 return ['table'];
219 return null;
224 $ctrl = new RelationController(
225 $this->response,
226 $this->template,
227 $GLOBALS['db'],
228 $GLOBALS['table'],
229 new Relation($GLOBALS['dbi'], $this->template),
230 $GLOBALS['dbi']
233 $_POST['foreign'] = 'false';
234 $ctrl->getDropdownValueForDatabase('INNODB');
235 $json = $this->response->getJSONResult();
236 $this->assertEquals(
237 ['table'],
238 $json['tables']