/tests/suite/joomla/html/JGridTest.php

https://github.com/pfisher3/joomla-platform · PHP · 571 lines · 414 code · 51 blank · 106 comment · 2 complexity · f17950311c6c967cef0d56d81808ff9e MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. jimport('joomla.html.grid');
  10. /**
  11. * General inspector class for JGrid.
  12. *
  13. * @package Joomla.UnitTest
  14. * @subpackage HTML
  15. * @since 11.3
  16. */
  17. class JGridInspector extends JGrid
  18. {
  19. /**
  20. * Method for inspecting protected variables.
  21. *
  22. * @return mixed The value of the class variable.
  23. */
  24. public function __get($name)
  25. {
  26. if (property_exists($this, $name)) {
  27. return $this->$name;
  28. } else {
  29. trigger_error('Undefined or private property: ' . __CLASS__.'::'.$name, E_USER_ERROR);
  30. return null;
  31. }
  32. }
  33. /**
  34. * Sets any property from the class.
  35. *
  36. * @param string $property The name of the class property.
  37. * @param string $value The value of the class property.
  38. *
  39. * @return void
  40. */
  41. public function __set($property, $value)
  42. {
  43. $this->$property = $value;
  44. }
  45. /**
  46. * Calls any inaccessible method from the class.
  47. *
  48. * @param string $name Name of the method to invoke
  49. * @param array $parameters Parameters to be handed over to the original method
  50. *
  51. * @return mixed The return value of the method
  52. */
  53. public function __call($name, $parameters = false)
  54. {
  55. return call_user_func_array(array($this,$name), $parameters);
  56. }
  57. }
  58. /**
  59. * Test class for JGrid.
  60. *
  61. * @since 11.3
  62. */
  63. class JGridTest extends PHPUnit_Framework_TestCase
  64. {
  65. /**
  66. * Test for JGrid::__construct method.
  67. */
  68. public function test__construct()
  69. {
  70. $table = new JGrid();
  71. $this->assertThat(
  72. ($table instanceof JGrid),
  73. $this->isTrue()
  74. );
  75. $options = array('class' => 'center', 'width' => '50%');
  76. $table = new JGridInspector($options);
  77. $this->assertThat(
  78. $table->options,
  79. $this->equalTo($options)
  80. );
  81. }
  82. /**
  83. * Test for JGrid::__toString method.
  84. */
  85. public function test__toString()
  86. {
  87. $table = new JGridInspector();
  88. $table->addColumn('testCol1');
  89. $table->addRow(array('class' => 'test1'));
  90. $table->setRowCell('testCol1', 'testcontent1', array('class' => '1'));
  91. $this->assertThat(
  92. (string) $table,
  93. $this->equalTo($table->toString())
  94. );
  95. }
  96. /**
  97. * Test for JGrid::setTableOptions method.
  98. */
  99. public function testSetTableOptions()
  100. {
  101. $options = array('class' => 'center', 'width' => '50%');
  102. $table = new JGridInspector();
  103. $table->setTableOptions($options);
  104. $this->assertThat(
  105. $table->options,
  106. $this->equalTo($options)
  107. );
  108. }
  109. /**
  110. * Test for JGrid::getTableOptions method.
  111. */
  112. public function testGetTableOptions()
  113. {
  114. $options = array('class' => 'center', 'width' => '50%');
  115. $table = new JGridInspector();
  116. $table->options = $options;
  117. $this->assertThat(
  118. $table->getTableOptions(),
  119. $this->equalTo($options)
  120. );
  121. }
  122. /**
  123. * Test for JGrid::addColumn method.
  124. */
  125. public function testAddColumn()
  126. {
  127. $table = new JGridInspector();
  128. $table->addColumn('test1');
  129. $this->assertThat(
  130. $table->columns,
  131. $this->equalTo(array('test1'))
  132. );
  133. }
  134. /**
  135. * Test for JGrid::getColumns method.
  136. */
  137. public function testGetColumns()
  138. {
  139. $table = new JGridInspector();
  140. $table->columns = array('test1');
  141. $this->assertThat(
  142. $table->getColumns(),
  143. $this->equalTo(array('test1'))
  144. );
  145. }
  146. /**
  147. * Test for JGrid::deleteColumn method.
  148. */
  149. public function testDeleteColumn()
  150. {
  151. $table = new JGridInspector();
  152. $table->columns = array('test1', 'test2', 'test3');
  153. $table->deleteColumn('test2');
  154. $this->assertThat(
  155. $table->columns,
  156. $this->equalTo(array('test1', 'test3'))
  157. );
  158. }
  159. /**
  160. * Test for JGrid::setColumns method.
  161. */
  162. public function testSetColumns()
  163. {
  164. $table = new JGridInspector();
  165. $table->columns = array('test1', 'test2', 'test3');
  166. $array = array('test4', 'test5');
  167. $table->setColumns($array);
  168. $this->assertThat(
  169. $table->columns,
  170. $this->equalTo(array('test4', 'test5'))
  171. );
  172. }
  173. /**
  174. * Test for JGrid::addRow method.
  175. */
  176. public function testAddRow()
  177. {
  178. $table = new JGridInspector();
  179. $table->addRow();
  180. $this->assertThat(
  181. $table->rows,
  182. $this->equalTo(array(0 => array('_row' => array())))
  183. );
  184. $this->assertThat(
  185. $table->activeRow,
  186. $this->equalTo(0)
  187. );
  188. $table->addRow();
  189. $this->assertThat(
  190. $table->rows,
  191. $this->equalTo(array(0 => array('_row' => array()), 1 => array('_row' => array())))
  192. );
  193. $this->assertThat(
  194. $table->activeRow,
  195. $this->equalTo(1)
  196. );
  197. $table->addRow(array('class' => 'test'));
  198. $this->assertThat(
  199. $table->rows,
  200. $this->equalTo(array(0 => array('_row' => array()), 1 => array('_row' => array()), 2 => array('_row' => array('class' => 'test'))))
  201. );
  202. $this->assertThat(
  203. $table->activeRow,
  204. $this->equalTo(2)
  205. );
  206. $table->addRow(array(), 1);
  207. $this->assertThat(
  208. $table->specialRows,
  209. $this->equalTo(array('header' => array(3), 'footer' => array()))
  210. );
  211. $table->addRow(array(), 2);
  212. $this->assertThat(
  213. $table->specialRows,
  214. $this->equalTo(array('header' => array(3), 'footer' => array(4)))
  215. );
  216. }
  217. /**
  218. * Test for JGrid::getRowOptions method.
  219. */
  220. public function testGetRowOptions()
  221. {
  222. $table = new JGridInspector();
  223. $table->rows = array(0 => array('_row' => array()));
  224. $table->activeRow = 0;
  225. $this->assertThat(
  226. $table->getRowOptions(),
  227. $this->equalTo(array())
  228. );
  229. $new = array('test' => 'test1');
  230. $table->rows = array(0 => array('_row' => $new));
  231. $this->assertThat(
  232. $table->getRowOptions(),
  233. $this->equalTo($new)
  234. );
  235. }
  236. /**
  237. * Test for JGrid::setRowOptions method.
  238. */
  239. public function testSetRowOptions()
  240. {
  241. $table = new JGridInspector();
  242. $table->rows = array(0 => array('_row' => array()));
  243. $table->activeRow = 0;
  244. $new = array('test' => 'test1');
  245. $table->setRowOptions($new);
  246. $this->assertThat(
  247. $table->rows[0]['_row'],
  248. $this->equalTo($new)
  249. );
  250. }
  251. /**
  252. * Test for JGrid::setActiveRow method.
  253. */
  254. public function testSetActiveRow()
  255. {
  256. $table = new JGridInspector();
  257. $table->rows = array(array('_row' => array('class' => 'test1')),
  258. array('_row' => array('class' => 'test2')),
  259. array('_row' => array('class' => 'test3')))
  260. ;
  261. $table->activeRow = 2;
  262. $table->setActiveRow(1);
  263. $this->assertThat(
  264. $table->activeRow,
  265. $this->equalTo(1)
  266. );
  267. }
  268. /**
  269. * Test for JGrid::getActiveRow method.
  270. */
  271. public function testGetActiveRow()
  272. {
  273. $table = new JGridInspector();
  274. $table->rows = array(array('_row' => array('class' => 'test1')),
  275. array('_row' => array('class' => 'test2')),
  276. array('_row' => array('class' => 'test3')))
  277. ;
  278. $table->activeRow = 2;
  279. $this->assertThat(
  280. $table->getActiveRow(),
  281. $this->equalTo(2)
  282. );
  283. $table->activeRow = 1;
  284. $this->assertThat(
  285. $table->getActiveRow(),
  286. $this->equalTo(1)
  287. );
  288. }
  289. /**
  290. * Test for JGrid::addRowCell method.
  291. */
  292. public function testSetRowCell()
  293. {
  294. $table = new JGridInspector();
  295. $table->columns = array('testCol1', 'testCol2', 'testCol3');
  296. $table->rows = array(array('_row' => array('class' => 'test1')),
  297. array('_row' => array('class' => 'test2')),
  298. array('_row' => array('class' => 'test3')))
  299. ;
  300. $assertion = new stdClass();
  301. $assertion->options = array();
  302. $assertion->content = 'testcontent3';
  303. $table->activeRow = 0;
  304. $table->setRowCell('testCol1', 'testcontent1', array('class' => '1'));
  305. $table->activeRow = 1;
  306. $table->setRowCell('testCol2', 'testcontent2');
  307. $table->activeRow = 2;
  308. $table->setRowCell('testCol3', 'testcontent3');
  309. $this->assertThat(
  310. $table->rows[2],
  311. $this->equalTo(array('_row' => array('class' => 'test3'), 'testCol3' => $assertion))
  312. );
  313. $assertion->content = 'testcontent2';
  314. $this->assertThat(
  315. $table->rows[1],
  316. $this->equalTo(array('_row' => array('class' => 'test2'), 'testCol2' => $assertion))
  317. );
  318. $assertion->content = 'testcontent1';
  319. $assertion->options = array('class' => '1');
  320. $this->assertThat(
  321. $table->rows[0],
  322. $this->equalTo(array('_row' => array('class' => 'test1'), 'testCol1' => $assertion))
  323. );
  324. //Test replacing existing content
  325. $table->activeRow = 0;
  326. $table->setRowCell('testCol1', 'testcontent4', array('test' => 'difcontent'));
  327. $assertion->content = 'testcontent4';
  328. $assertion->options = array('test' => 'difcontent');
  329. $this->assertThat(
  330. $table->rows[0],
  331. $this->equalTo(array('_row' => array('class' => 'test1'), 'testCol1' => $assertion))
  332. );
  333. //Test appending content
  334. $table->setRowCell('testCol1', ' appendedcontent', array('class' => '1'), false);
  335. $assertion->content = 'testcontent4 appendedcontent';
  336. $assertion->options = array('class' => '1');
  337. $this->assertThat(
  338. $table->rows[0],
  339. $this->equalTo(array('_row' => array('class' => 'test1'), 'testCol1' => $assertion))
  340. );
  341. //Test adding another cell
  342. $table->setRowCell('testCol2', 'Col2content');
  343. $assertion2 = new stdClass();
  344. $assertion2->content = 'Col2content';
  345. $assertion2->options = array();
  346. $this->assertThat(
  347. $table->rows[0],
  348. $this->equalTo(array('_row' => array('class' => 'test1'), 'testCol1' => $assertion, 'testCol2' => $assertion2))
  349. );
  350. }
  351. /**
  352. * Test for JGrid::getRow method.
  353. */
  354. public function testGetRow()
  355. {
  356. $table = new JGridInspector();
  357. $table->columns = array('testCol1');
  358. $table->rows = array(0 => array('_row' => array('ref' => 'idtest')), 1 => array('_row' => array('class' => 'test1')));
  359. $table->activeRow = 1;
  360. $this->assertThat(
  361. $table->getRow(),
  362. $this->equalTo(array('_row' => array('class' => 'test1')))
  363. );
  364. $this->assertThat(
  365. $table->getRow(0),
  366. $this->equalTo(array('_row' => array('ref' => 'idtest')))
  367. );
  368. }
  369. /**
  370. * Test for JGrid::getRows method.
  371. */
  372. public function testGetRows()
  373. {
  374. $table = new JGridInspector();
  375. $table->columns = array('testCol1');
  376. $assertion = new stdClass();
  377. $assertion->options = array('class' => '1');
  378. $assertion->content = 'testcontent1';
  379. $table->rows = array(
  380. 0 => array('_row' => array('class' => 'test1'), 'testCol1' => $assertion),
  381. 1 => array('_row' => array('class' => 'test2'), 'testCol1' => $assertion),
  382. 2 => array('_row' => array('class' => 'test3'), 'testCol1' => $assertion)
  383. );
  384. $table->specialRows = array('header' => array(1), 'footer' => array(2));
  385. $this->assertThat(
  386. $table->getRows(),
  387. $this->equalTo(array(0))
  388. );
  389. $this->assertThat(
  390. $table->getRows(1),
  391. $this->equalTo(array(1))
  392. );
  393. $this->assertThat(
  394. $table->getRows(2),
  395. $this->equalTo(array(2))
  396. );
  397. }
  398. /**
  399. * Test for JGrid::deleteRow method.
  400. */
  401. public function testDeleteRow()
  402. {
  403. $table = new JGridInspector();
  404. $table->columns = array('testCol1');
  405. $assertion = new stdClass();
  406. $assertion->options = array('class' => '1');
  407. $assertion->content = 'testcontent1';
  408. $table->rows = array(
  409. 0 => array('_row' => array('class' => 'test1'), 'testCol1' => $assertion),
  410. 1 => array('_row' => array('class' => 'test2'), 'testCol1' => $assertion),
  411. 2 => array('_row' => array('class' => 'test3'), 'testCol1' => $assertion)
  412. );
  413. $table->specialRows = array('header' => array(1), 'footer' => array(2));
  414. $table->deleteRow(0);
  415. $this->assertThat(
  416. $table->rows,
  417. $this->equalTo(array(
  418. 1 => array('_row' => array('class' => 'test2'), 'testCol1' => $assertion),
  419. 2 => array('_row' => array('class' => 'test3'), 'testCol1' => $assertion)
  420. ))
  421. );
  422. $this->assertThat(
  423. $table->getRow(),
  424. $this->equalTo(array('_row' => array('class' => 'test3'), 'testCol1' => $assertion))
  425. );
  426. $this->assertThat(
  427. $table->specialRows,
  428. $this->equalTo(array('header' => array(1), 'footer' => array(2)))
  429. );
  430. $table->deleteRow(1);
  431. $this->assertThat(
  432. $table->rows,
  433. $this->equalTo(array(
  434. 2 => array('_row' => array('class' => 'test3'), 'testCol1' => $assertion)
  435. ))
  436. );
  437. $this->assertThat(
  438. $table->getRow(),
  439. $this->equalTo(array('_row' => array('class' => 'test3'), 'testCol1' => $assertion))
  440. );
  441. $this->assertThat(
  442. $table->specialRows,
  443. $this->equalTo(array('header' => array(), 'footer' => array(2)))
  444. );
  445. $table->deleteRow(2);
  446. $this->assertThat(
  447. $table->rows,
  448. $this->equalTo(array())
  449. );
  450. $this->assertThat(
  451. $table->getRow(),
  452. $this->equalTo(false)
  453. );
  454. $this->assertThat(
  455. $table->specialRows,
  456. $this->equalTo(array('header' => array(), 'footer' => array()))
  457. );
  458. }
  459. /**
  460. * Test for JGrid::toString method.
  461. */
  462. public function testToString()
  463. {
  464. $table = new JGridInspector();
  465. $table->columns = array('testCol1');
  466. $assertion = new stdClass();
  467. $assertion->options = array('class' => '1');
  468. $assertion->content = 'testcontent1';
  469. $table->rows = array(array('_row' => array('class' => 'test1'), 'testCol1' => $assertion));
  470. //Make sure the body is rendered correctly
  471. $this->assertThat(
  472. $table->toString(),
  473. $this->equalTo("<table><tbody>\n\t<tr class=\"test1\">\n\t\t<td class=\"1\">testcontent1</td>\n\t</tr>\n</tbody></table>")
  474. );
  475. //Make sure the header is rendered correctly
  476. $table->specialRows = array('header' => array(0), 'footer' => array());
  477. $this->assertThat(
  478. $table->toString(),
  479. $this->equalTo("<table><thead>\n\t<tr class=\"test1\">\n\t\t<th class=\"1\">testcontent1</th>\n\t</tr>\n</thead></table>")
  480. );
  481. //Make sure the footer is rendered correctly
  482. $table->specialRows = array('header' => array(), 'footer' => array(0));
  483. $this->assertThat(
  484. $table->toString(),
  485. $this->equalTo("<table><tfoot>\n\t<tr class=\"test1\">\n\t\t<td class=\"1\">testcontent1</td>\n\t</tr>\n</tfoot></table>")
  486. );
  487. }
  488. /**
  489. * Test for JGrid::renderArea method.
  490. */
  491. public function testRenderArea()
  492. {
  493. $table = new JGridInspector();
  494. $table->columns = array('testCol1');
  495. $content = new stdClass();
  496. $content->options = array('class' => 'test1');
  497. $content->content = 'testcontent';
  498. $table->rows = array(0 => array('_row' => array(), 'testCol1' => $content));
  499. $table->specialRows = array('header' => array(0), 'footer' => array());
  500. $this->assertThat(
  501. $table->renderArea(array(0), 'thead', 'th'),
  502. $this->equalTo("<thead>\n\t<tr>\n\t\t<th class=\"test1\">testcontent</th>\n\t</tr>\n</thead>")
  503. );
  504. }
  505. /**
  506. * Test for JGrid::renderAttributes method.
  507. */
  508. public function testRenderAttributes()
  509. {
  510. $table = new JGridInspector();
  511. $this->assertThat(
  512. $table->renderAttributes(array('class' => 'test1')),
  513. $this->equalTo(' class="test1"')
  514. );
  515. $this->assertThat(
  516. $table->renderAttributes(array('class' => 'test1', 'ref' => 'test5')),
  517. $this->equalTo(' class="test1" ref="test5"')
  518. );
  519. }
  520. }