PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/suite/joomla/form/JFormTest.php

http://github.com/joomla/joomla-platform
PHP | 2200 lines | 1554 code | 364 blank | 282 comment | 8 complexity | 50ea915218ff6b4d4afaf8db468ed90e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. /**
  10. * Test class for JForm.
  11. *
  12. * @package Joomla.UnitTest
  13. * @subpackage Form
  14. */
  15. class JFormTest extends JoomlaTestCase
  16. {
  17. private function _showXml($form)
  18. {
  19. $dom = new DOMDocument('1.0');
  20. $dom->preserveWhiteSpace = false;
  21. $dom->formatOutput = true;
  22. $dom->loadXML($form->getXml()->asXML());
  23. echo $dom->saveXML();
  24. }
  25. /**
  26. * set up for testing
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. $this->saveFactoryState();
  33. jimport('joomla.utilities.xmlelement');
  34. include_once 'inspectors.php';
  35. include_once 'JFormDataHelper.php';
  36. }
  37. /**
  38. * Tear down test
  39. *
  40. * @return void
  41. */
  42. function tearDown()
  43. {
  44. $this->restoreFactoryState();
  45. }
  46. /**
  47. * Tests the JForm::addFieldPath method.
  48. *
  49. * This method is used to add additional lookup paths for field helpers.
  50. */
  51. public function testAddFieldPath()
  52. {
  53. // Check the default behaviour.
  54. $paths = JForm::addFieldPath();
  55. // The default path is the class file folder/forms
  56. $valid = JPATH_PLATFORM . '/joomla/form/fields';
  57. $this->assertThat(
  58. in_array($valid, $paths),
  59. $this->isTrue(),
  60. 'Line:'.__LINE__.' The libraries fields path should be included by default.'
  61. );
  62. // Test adding a custom folder.
  63. JForm::addFieldPath(__DIR__);
  64. $paths = JForm::addFieldPath();
  65. $this->assertThat(
  66. in_array(__DIR__, $paths),
  67. $this->isTrue(),
  68. 'Line:'.__LINE__.' An added path should be in the returned array.'
  69. );
  70. }
  71. /**
  72. * Tests the JForm::addFormPath method.
  73. *
  74. * This method is used to add additional lookup paths for form XML files.
  75. */
  76. public function testAddFormPath()
  77. {
  78. // Check the default behaviour.
  79. $paths = JForm::addFormPath();
  80. // The default path is the class file folder/forms
  81. $valid = JPATH_PLATFORM . '/joomla/form/forms';
  82. $this->assertThat(
  83. in_array($valid, $paths),
  84. $this->isTrue(),
  85. 'Line:'.__LINE__.' The libraries forms path should be included by default.'
  86. );
  87. // Test adding a custom folder.
  88. JForm::addFormPath(__DIR__);
  89. $paths = JForm::addFormPath();
  90. $this->assertThat(
  91. in_array(__DIR__, $paths),
  92. $this->isTrue(),
  93. 'Line:'.__LINE__.' An added path should be in the returned array.'
  94. );
  95. }
  96. /**
  97. * Tests the JForm::addRulePath method.
  98. *
  99. * This method is used to add additional lookup paths for form XML files.
  100. */
  101. public function testAddRulePath()
  102. {
  103. // Check the default behaviour.
  104. $paths = JForm::addRulePath();
  105. // The default path is the class file folder/rules
  106. $valid = JPATH_PLATFORM . '/joomla/form/rules';
  107. $this->assertThat(
  108. in_array($valid, $paths),
  109. $this->isTrue(),
  110. 'Line:'.__LINE__.' The libraries rule path should be included by default.'
  111. );
  112. // Test adding a custom folder.
  113. JForm::addRulePath(__DIR__);
  114. $paths = JForm::addRulePath();
  115. $this->assertThat(
  116. in_array(__DIR__, $paths),
  117. $this->isTrue(),
  118. 'Line:'.__LINE__.' An added path should be in the returned array.'
  119. );
  120. }
  121. /**
  122. * Test the JForm::addNode method.
  123. */
  124. public function testAddNode()
  125. {
  126. // The source data.
  127. $xml1 = simplexml_load_string('<form><fields /></form>', 'JXMLElement');
  128. // The new data for adding the field.
  129. $xml2 = simplexml_load_string('<form><field name="foo" /></form>', 'JXMLElement');
  130. if ($xml1 === false || $xml2 === false) {
  131. $this->fail('Error in text XML data');
  132. }
  133. JFormInspector::addNode($xml1->fields, $xml2->field);
  134. $fields = $xml1->xpath('fields/field[@name="foo"]');
  135. $this->assertThat(
  136. count($fields),
  137. $this->equalTo(1),
  138. 'Line:'.__LINE__.' The field should be added, ungrouped.'
  139. );
  140. }
  141. /**
  142. * Tests the JForm::bind method.
  143. *
  144. * This method is used to load data into the JForm object.
  145. */
  146. public function testBind()
  147. {
  148. $form = new JFormInspector('form1');
  149. $xml = JFormDataHelper::$bindDocument;
  150. // Check the test data loads ok.
  151. $this->assertThat(
  152. $form->load($xml),
  153. $this->isTrue(),
  154. 'Line:'.__LINE__.' XML string should load successfully.'
  155. );
  156. $data = array(
  157. 'title' => 'Joomla Framework',
  158. 'author' => 'Should not bind',
  159. 'params' => array(
  160. 'show_title' => 1,
  161. 'show_abstract' => 0,
  162. 'show_author' => 1,
  163. 'categories' => array(
  164. 1,
  165. 2
  166. ),
  167. 'keywords' => array('en-GB'=>'Joomla', 'fr-FR'=>'Joomla')
  168. )
  169. );
  170. $this->assertThat(
  171. $form->bind($data),
  172. $this->isTrue(),
  173. 'Line:'.__LINE__.' The data should bind successfully.'
  174. );
  175. $data = $form->getData();
  176. $this->assertThat(
  177. $data->get('title'),
  178. $this->equalTo('Joomla Framework'),
  179. 'Line:'.__LINE__.' The data should bind to form field elements.'
  180. );
  181. $this->assertThat(
  182. $data->get('author'),
  183. $this->isNull(),
  184. 'Line:'.__LINE__.' The data should not bind to unknown form field elements.'
  185. );
  186. $this->assertThat(
  187. is_array($data->get('params.categories')),
  188. $this->isTrue(),
  189. 'Line:'.__LINE__.' The categories param should be an array.'
  190. );
  191. }
  192. /**
  193. * Testing methods used by the instantiated object.
  194. *
  195. * @return void
  196. */
  197. public function testConstruct()
  198. {
  199. $form = new JFormInspector('form1');
  200. $this->assertThat(
  201. ($form instanceof JForm),
  202. $this->isTrue(),
  203. 'Line:'.__LINE__.' The JForm constuctor should return a JForm object.'
  204. );
  205. // Check the integrity of the options.
  206. $options = $form->getOptions();
  207. $this->assertThat(
  208. isset($options['control']),
  209. $this->isTrue(),
  210. 'Line:'.__LINE__.' The JForm object should contain an options array with a control setting.'
  211. );
  212. $options = $form->getOptions();
  213. $this->assertThat(
  214. $options['control'],
  215. $this->isFalse(),
  216. 'Line:'.__LINE__.' The control setting should be false by default.'
  217. );
  218. $form = new JFormInspector('form1', array('control' => 'jform'));
  219. $options = $form->getOptions();
  220. $this->assertThat(
  221. $options['control'],
  222. $this->equalTo('jform'),
  223. 'Line:'.__LINE__.' The control setting should be what is passed in the constructor.'
  224. );
  225. }
  226. /**
  227. * Test for JForm::filter method.
  228. *
  229. * @return void
  230. */
  231. public function testFilter()
  232. {
  233. $form = new JFormInspector('form1');
  234. // Check the test data loads ok.
  235. $this->assertThat(
  236. $form->load(JFormDataHelper::$filterDocument),
  237. $this->isTrue(),
  238. 'Line:'.__LINE__.' XML string should load successfully.'
  239. );
  240. $data = array(
  241. 'word' => 'Joomla! Framework',
  242. 'author' => 'Should not bind',
  243. 'params' => array(
  244. 'show_title' => 1,
  245. 'show_author' => false,
  246. ),
  247. 'default' => ''
  248. );
  249. $filtered = $form->filter($data);
  250. $this->assertThat(
  251. is_array($filtered),
  252. $this->isTrue(),
  253. 'Line:'.__LINE__.' The filtered result should be an array.'
  254. );
  255. // Test that filtering is occuring (not that all filters work - done in testFilterField).
  256. $this->assertThat(
  257. $filtered['word'],
  258. $this->equalTo('JoomlaFramework'),
  259. 'Line:'.__LINE__.' The variable should be filtered by the "word" filter.'
  260. );
  261. $this->assertThat(
  262. isset($filtered['author']),
  263. $this->isFalse(),
  264. 'Line:'.__LINE__.' A variable in the data not present in the form should not exist.'
  265. );
  266. $this->assertThat(
  267. $filtered['params']['show_title'],
  268. $this->equalTo(1),
  269. 'Line:'.__LINE__.' The nested variable should be present.'
  270. );
  271. $this->assertThat(
  272. $filtered['params']['show_author'],
  273. $this->equalTo(0),
  274. 'Line:'.__LINE__.' The nested variable should be present.'
  275. );
  276. }
  277. /**
  278. * Test for JForm::filterField method.
  279. */
  280. public function testFilterField()
  281. {
  282. $form = new JFormInspector('form1');
  283. // Check the test data loads ok.
  284. $this->assertThat(
  285. $form->load(JFormDataHelper::$filterDocument),
  286. $this->isTrue(),
  287. 'Line:'.__LINE__.' XML string should load successfully.'
  288. );
  289. $input = '<script>alert();</script> <p>Some text.</p>';
  290. $this->assertThat(
  291. $form->filterField($form->findField('function'), $input),
  292. $this->equalTo('function'),
  293. 'Line:'.__LINE__.' The function filter should be correctly applied.'
  294. );
  295. $this->assertThat(
  296. $form->filterField($form->findField('int'), 'A1B2C3'),
  297. $this->equalTo(1),
  298. 'Line:'.__LINE__.' The "int" filter should be correctly applied.'
  299. );
  300. $this->assertThat(
  301. $form->filterField($form->findField('method'), $input),
  302. $this->equalTo('method'),
  303. 'Line:'.__LINE__.' The class method filter should be correctly applied.'
  304. );
  305. $this->assertThat(
  306. $form->filterField($form->findField('raw'), $input),
  307. $this->equalTo($input),
  308. 'Line:'.__LINE__.' "The safehtml" filter should be correctly applied.'
  309. );
  310. $this->assertThat(
  311. $form->filterField($form->findField('safehtml'), $input),
  312. $this->equalTo('alert(); <p>Some text.</p>'),
  313. 'Line:'.__LINE__.' "The safehtml" filter should be correctly applied.'
  314. );
  315. $this->assertThat(
  316. $form->filterField($form->findField('unset'), $input),
  317. $this->equalTo(null),
  318. 'Line:'.__LINE__.' The value should be unset.'
  319. );
  320. $this->assertThat(
  321. $form->filterField($form->findField('word'), $input),
  322. $this->equalTo('scriptalertscriptpSometextp'),
  323. 'Line:'.__LINE__.' The "word" filter should be correctly applied.'
  324. );
  325. $this->assertThat(
  326. $form->filterField($form->findField('url'), 'http://example.com'),
  327. $this->equalTo('http://example.com'),
  328. 'Line:'.__LINE__.' A field with a valid protocol should return as is.'
  329. );
  330. $this->assertThat(
  331. $form->filterField($form->findField('url'), 'http://<script>alert();</script> <p>Some text.</p>'),
  332. $this->equalTo('http://alert(); Some text.'),
  333. 'Line:'.__LINE__.' A "url" with scripts should be should be filtered.'
  334. );
  335. $this->assertThat(
  336. $form->filterField($form->findField('url'), 'https://example.com'),
  337. $this->equalTo('https://example.com'),
  338. 'Line:'.__LINE__.' A field with a valid protocol that is not http should return as is.'
  339. );
  340. $this->assertThat(
  341. $form->filterField($form->findField('url'), 'example.com'),
  342. $this->equalTo('http://example.com'),
  343. 'Line:'.__LINE__.' A field without a protocol should return with a http:// protocol.'
  344. );
  345. $this->assertThat(
  346. $form->filterField($form->findField('url'), 'hptarr.com'),
  347. $this->equalTo('http://hptarr.com'),
  348. 'Line:'.__LINE__.' A field without a protocol and starts with t should return with a http:// protocol.'
  349. );
  350. $this->assertThat(
  351. $form->filterField($form->findField('url'), ''),
  352. $this->equalTo(''),
  353. 'Line:'.__LINE__.' An empty "url" filter return nothing.'
  354. );
  355. $this->assertThat(
  356. $form->filterField($form->findField('default'), $input),
  357. $this->equalTo('alert(); Some text.'),
  358. 'Line:'.__LINE__.' The default strict filter should be correctly applied.'
  359. );
  360. $this->assertThat(
  361. $form->filterField($form->findField('tel'), '222.3333333333'),
  362. $this->equalTo('222.3333333333'),
  363. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  364. );
  365. $this->assertThat(
  366. $form->filterField($form->findField('tel'), '+222.3333333333'),
  367. $this->equalTo('222.3333333333'),
  368. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  369. );
  370. $this->assertThat(
  371. $form->filterField($form->findField('tel'), '+2,2,2.3,3,3,3,3,3,3,3,3,3,3,3'),
  372. $this->equalTo('222.333333333333'),
  373. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  374. );
  375. $this->assertThat(
  376. $form->filterField($form->findField('tel'), '33333333333'),
  377. $this->equalTo('.33333333333'),
  378. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  379. );
  380. $this->assertThat(
  381. $form->filterField($form->findField('tel'), '222333333333333'),
  382. $this->equalTo('222.333333333333'),
  383. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  384. );
  385. $this->assertThat(
  386. $form->filterField($form->findField('tel'), '1 (202) 555-5555'),
  387. $this->equalTo('1.2025555555'),
  388. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  389. );
  390. $this->assertThat(
  391. $form->filterField($form->findField('tel'), '+222.33333333333x444'),
  392. $this->equalTo('222.33333333333'),
  393. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  394. );
  395. $this->assertThat(
  396. $form->filterField($form->findField('tel'), 'ABCabc/?.!*x'),
  397. $this->equalTo(''),
  398. 'Line:'.__LINE__.' The tel filter should be correctly applied.'
  399. );
  400. $this->assertThat(
  401. $form->filterField($form->findField('server_utc'), 'foo'),
  402. $this->equalTo(''),
  403. 'Line:'.__LINE__.' A non-date for a server_utc filter should return nothing.'
  404. );
  405. $this->assertThat(
  406. $form->filterField($form->findField('server_utc'), ''),
  407. $this->equalTo(''),
  408. 'Line:'.__LINE__.' An empty date for a server_utc filter should return nothing.'
  409. );
  410. $this->assertThat(
  411. $form->filterField($form->findField('user_utc'), 'foo'),
  412. $this->equalTo(''),
  413. 'Line:'.__LINE__.' A non-date for a user_utc filter should return nothing.'
  414. );
  415. $this->assertThat(
  416. $form->filterField($form->findField('user_utc'), ''),
  417. $this->equalTo(''),
  418. 'Line:'.__LINE__.' An empty date for a user_utc filter should return nothing.'
  419. );
  420. $this->markTestIncomplete('Need to deal with SERVER_UTC and USER_UTC filters');
  421. /*
  422. include_once JPATH_BASE . '/libraries/joomla/user/user.php';
  423. $user = new JUser;
  424. $mockSession = $this->getMock('JSession', array('_start', 'get'));
  425. $mockSession->expects($this->once())->method('get')->will(
  426. $this->returnValue($user)
  427. );
  428. JFactory::$session = $mockSession;
  429. // Adjust the timezone offset to a known value.
  430. $config = JFactory::getConfig();
  431. $config->setValue('config.offset', 10);
  432. // TODO: Mock JFactory and JUser
  433. //$user = JFactory::getUser();
  434. //$user->setParam('timezone', 5);
  435. $form = new JForm;
  436. $form->load('example');
  437. $text = '<script>alert();</script> <p>Some text</p>';
  438. $data = array(
  439. 'f_svr_date' => '2009-01-01 00:00:00',
  440. 'f_usr_date' => '2009-01-01 00:00:00',
  441. );
  442. // Check the date filters.
  443. $this->assertThat(
  444. $result['f_svr_date'],
  445. $this->equalTo('2008-12-31 14:00:00')
  446. );
  447. //$this->assertThat(
  448. // $result['f_usr_date'],
  449. // $this->equalTo('2009-01-01 05:00:00')
  450. //);
  451. */
  452. }
  453. /**
  454. * Test the JForm::findField method.
  455. */
  456. public function testFindField()
  457. {
  458. // Prepare the form.
  459. $form = new JFormInspector('form1');
  460. $xml = JFormDataHelper::$findFieldDocument;
  461. // Check the test data loads ok.
  462. $this->assertThat(
  463. $form->load($xml),
  464. $this->isTrue(),
  465. 'Line:'.__LINE__.' XML string should load successfully.'
  466. );
  467. // Error handling.
  468. $this->assertThat(
  469. $form->findField('bogus'),
  470. $this->isFalse(),
  471. 'Line:'.__LINE__.' An ungrouped field that does not exist should return false.'
  472. );
  473. $this->assertThat(
  474. $form->findField('title', 'bogus'),
  475. $this->isFalse(),
  476. 'Line:'.__LINE__.' An field in a group that does not exist should return false.'
  477. );
  478. // Test various find combinations.
  479. $field = $form->findField('title', null);
  480. $this->assertThat(
  481. (string) $field['place'],
  482. $this->equalTo('root'),
  483. 'Line:'.__LINE__.' A known ungrouped field should load successfully.'
  484. );
  485. $field = $form->findField('title', 'params');
  486. $this->assertThat(
  487. (string) $field['place'],
  488. $this->equalTo('child'),
  489. 'Line:'.__LINE__.' A known grouped field should load successfully.'
  490. );
  491. $field = $form->findField('alias');
  492. $this->assertThat(
  493. (string) $field['name'],
  494. $this->equalTo('alias'),
  495. 'Line:'.__LINE__.' A known field in a fieldset should load successfully.'
  496. );
  497. $field = $form->findField('show_title', 'params');
  498. $this->assertThat(
  499. (string) $field['default'],
  500. $this->equalTo('1'),
  501. 'Line:'.__LINE__.' A known field in a group fieldset should load successfully.'
  502. );
  503. }
  504. /**
  505. * Tests the JForm::findFieldsByFieldset method.
  506. */
  507. public function testFindFieldsByFieldset()
  508. {
  509. // Prepare the form.
  510. $form = new JFormInspector('form1');
  511. $this->assertThat(
  512. $form->load(JFormDataHelper::$findFieldsByFieldsetDocument),
  513. $this->isTrue(),
  514. 'Line:'.__LINE__.' XML string should load successfully.'
  515. );
  516. // Error handling.
  517. $this->assertThat(
  518. $form->findFieldsByFieldset('bogus'),
  519. $this->equalTo(array()),
  520. 'Line:'.__LINE__.' An unknown fieldset should return an empty array.'
  521. );
  522. // Test regular usage.
  523. $this->assertThat(
  524. count($form->findFieldsByFieldset('params-basic')),
  525. $this->equalTo(3),
  526. 'Line:'.__LINE__.' The params-basic fieldset has 3 fields.'
  527. );
  528. $this->assertThat(
  529. count($form->findFieldsByFieldset('params-advanced')),
  530. $this->equalTo(2),
  531. 'Line:'.__LINE__.' The params-advanced fieldset has 2 fields.'
  532. );
  533. }
  534. /**
  535. * Test the JForm::findFieldsByGroup method.
  536. */
  537. public function testFindFieldsByGroup()
  538. {
  539. // Prepare the form.
  540. $form = new JFormInspector('form1');
  541. $this->assertThat(
  542. $form->load(JFormDataHelper::$findFieldsByGroupDocument),
  543. $this->isTrue(),
  544. 'Line:'.__LINE__.' XML string should load successfully.'
  545. );
  546. // Error handling.
  547. $this->assertThat(
  548. $form->findFieldsByGroup('bogus'),
  549. $this->equalTo(array()),
  550. 'Line:'.__LINE__.' A group that does not exist should return an empty array.'
  551. );
  552. // Test all fields.
  553. $this->assertThat(
  554. count($form->findFieldsByGroup()),
  555. $this->equalTo(11),
  556. 'Line:'.__LINE__.' There are 9 field elements in total.'
  557. );
  558. // Test ungrouped fields.
  559. $this->assertThat(
  560. count($form->findFieldsByGroup(false)),
  561. $this->equalTo(4),
  562. 'Line:'.__LINE__.' There are 4 ungrouped field elements.'
  563. );
  564. // Test grouped fields.
  565. $this->assertThat(
  566. count($form->findFieldsByGroup('details')),
  567. $this->equalTo(2),
  568. 'Line:'.__LINE__.' The details group has 2 field elements.'
  569. );
  570. $this->assertThat(
  571. count($form->findFieldsByGroup('params')),
  572. $this->equalTo(3),
  573. 'Line:'.__LINE__.' The params group has 3 field elements, including one nested in a fieldset.'
  574. );
  575. // Test nested fields.
  576. $this->assertThat(
  577. count($form->findFieldsByGroup('level1', true)),
  578. $this->equalTo(2),
  579. 'Line:'.__LINE__.' There should be 2 nested fields.'
  580. );
  581. }
  582. /**
  583. * Test the JForm::findGroup method.
  584. */
  585. public function testFindGroup()
  586. {
  587. // Prepare the form.
  588. $form = new JFormInspector('form1');
  589. $this->assertThat(
  590. $form->load(JFormDataHelper::$findGroupDocument),
  591. $this->isTrue(),
  592. 'Line:'.__LINE__.' XML string should load successfully.'
  593. );
  594. $this->assertThat(
  595. $form->findGroup('bogus'),
  596. $this->equalTo(array()),
  597. 'Line:'.__LINE__.' A group that does not exist should return an empty array.'
  598. );
  599. $this->assertThat(
  600. count($form->findGroup('params')),
  601. $this->equalTo(1),
  602. 'Line:'.__LINE__.' The group should have one element.'
  603. );
  604. $this->assertThat(
  605. $form->findGroup('bogus.data'),
  606. $this->equalTo(array()),
  607. 'Line:'.__LINE__.' A group path that does not exist should return an empty array.'
  608. );
  609. // Check that an existant field returns something.
  610. $this->assertThat(
  611. count($form->findGroup('params.cache')),
  612. $this->equalTo(1),
  613. 'Line:'.__LINE__.' The group should have one element.'
  614. );
  615. }
  616. /**
  617. * Test for JForm::getErrors method.
  618. */
  619. public function testGetErrors()
  620. {
  621. $form = new JFormInspector('form1');
  622. $this->assertThat(
  623. $form->load(JFormDataHelper::$validateDocument),
  624. $this->isTrue(),
  625. 'Line:'.__LINE__.' XML string should load successfully.'
  626. );
  627. $fail = array(
  628. 'boolean' => 'comply',
  629. 'required' => '',
  630. );
  631. $this->assertThat(
  632. $form->validate($fail),
  633. $this->isFalse(),
  634. 'Line:'.__LINE__.' Validating this data should fail.'
  635. );
  636. $errors = $form->getErrors($fail);
  637. $this->assertThat(
  638. count($errors),
  639. $this->equalTo(3),
  640. 'Line:'.__LINE__.' This data should invoke 3 errors.'
  641. );
  642. $this->assertThat(
  643. $errors[0] instanceof JException,
  644. $this->isTrue(),
  645. 'Line:'.__LINE__.' The errors should be exception objects.'
  646. );
  647. }
  648. /**
  649. * Test the JForm::getField method.
  650. */
  651. public function testGetField()
  652. {
  653. // Prepare the form.
  654. $form = new JFormInspector('form1');
  655. $this->assertThat(
  656. $form->load(JFormDataHelper::$getFieldDocument),
  657. $this->isTrue(),
  658. 'Line:'.__LINE__.' XML string should load successfully.'
  659. );
  660. // Check for errors.
  661. $this->assertThat(
  662. $form->getField('bogus'),
  663. $this->isFalse(),
  664. 'Line:'.__LINE__.' A field that does not exist should return false.'
  665. );
  666. $this->assertThat(
  667. $form->getField('show_title'),
  668. $this->isFalse(),
  669. 'Line:'.__LINE__.' A field that does exists in a group, without declaring the group, should return false.'
  670. );
  671. $this->assertThat(
  672. $form->getField('show_title', 'bogus'),
  673. $this->isFalse(),
  674. 'Line:'.__LINE__.' A field in a group that does not exist should return false.'
  675. );
  676. // Checking value defaults.
  677. $this->assertThat(
  678. $form->getField('title')->value,
  679. $this->equalTo(''),
  680. 'Line:'.__LINE__.' Prior to binding data, the defaults in the field should be used.'
  681. );
  682. $this->assertThat(
  683. $form->getField('show_title', 'params')->value,
  684. $this->equalTo(1),
  685. 'Line:'.__LINE__.' Prior to binding data, the defaults in the field should be used.'
  686. );
  687. // Check values after binding.
  688. $data = array(
  689. 'title' => 'The title',
  690. 'show_title' => 3,
  691. 'params' => array(
  692. 'show_title' => 2,
  693. )
  694. );
  695. $this->assertThat(
  696. $form->bind($data),
  697. $this->isTrue(),
  698. 'Line:'.__LINE__.' The input data should bind successfully.'
  699. );
  700. $this->assertThat(
  701. $form->getField('title')->value,
  702. $this->equalTo('The title'),
  703. 'Line:'.__LINE__.' Check the field value bound correctly.'
  704. );
  705. $this->assertThat(
  706. $form->getField('show_title', 'params')->value,
  707. $this->equalTo(2),
  708. 'Line:'.__LINE__.' Check the field value bound correctly.'
  709. );
  710. // Check binding with an object.
  711. $data = new stdClass;
  712. $data->title = 'The new title';
  713. $data->show_title = 5;
  714. $data->params = new stdClass;
  715. $data->params->show_title = 4;
  716. $this->assertThat(
  717. $form->bind($data),
  718. $this->isTrue(),
  719. 'Line:'.__LINE__.' The input data should bind successfully.'
  720. );
  721. $this->assertThat(
  722. $form->getField('title')->value,
  723. $this->equalTo('The new title'),
  724. 'Line:'.__LINE__.' Check the field value bound correctly.'
  725. );
  726. $this->assertThat(
  727. $form->getField('show_title', 'params')->value,
  728. $this->equalTo(4),
  729. 'Line:'.__LINE__.' Check the field value bound correctly.'
  730. );
  731. }
  732. /**
  733. * Test for JForm::getFieldAttribute method.
  734. */
  735. public function testGetFieldAttribute()
  736. {
  737. $form = new JFormInspector('form1');
  738. $this->assertThat(
  739. $form->load(JFormDataHelper::$getFieldDocument),
  740. $this->isTrue(),
  741. 'Line:'.__LINE__.' XML string should load successfully.'
  742. );
  743. // Test error handling.
  744. $this->assertThat(
  745. $form->getFieldAttribute('bogus', 'unknown', 'Help'),
  746. $this->equalTo('Help'),
  747. 'Line:'.__LINE__.' The default value of the unknown field should be returned.'
  748. );
  749. $this->assertThat(
  750. $form->getFieldAttribute('title', 'unknown', 'Use this'),
  751. $this->equalTo('Use this'),
  752. 'Line:'.__LINE__.' The default value of the unknown attribute should be returned.'
  753. );
  754. // Test general usage.
  755. $this->assertThat(
  756. $form->getFieldAttribute('title', 'description'),
  757. $this->equalTo('The title.'),
  758. 'Line:'.__LINE__.' The value of the attribute should be returned.'
  759. );
  760. $this->assertThat(
  761. $form->getFieldAttribute('title', 'description', 'Use this'),
  762. $this->equalTo('The title.'),
  763. 'Line:'.__LINE__.' The value of the attribute should be returned.'
  764. );
  765. }
  766. /**
  767. * Test the JForm::getFormControl method.
  768. */
  769. public function testGetFormControl()
  770. {
  771. $form = new JForm('form8ion');
  772. $this->assertThat(
  773. $form->getFormControl(),
  774. $this->equalTo(''),
  775. 'Line:'.__LINE__.' A form control that has not been specified should return nothing.'
  776. );
  777. $form = new JForm('form8ion', array('control' => 'jform'));
  778. $this->assertThat(
  779. $form->getFormControl(),
  780. $this->equalTo('jform'),
  781. 'Line:'.__LINE__.' The form control should agree with the options passed in the constructor.'
  782. );
  783. }
  784. /**
  785. * Test for JForm::getInstance.
  786. */
  787. public function testGetInstance()
  788. {
  789. $this->markTestIncomplete();
  790. }
  791. /**
  792. * Test for JForm::getGroup method.
  793. */
  794. public function testGetGroup()
  795. {
  796. $form = new JFormInspector('form1');
  797. $this->assertThat(
  798. $form->load(JFormDataHelper::$findFieldsByGroupDocument),
  799. $this->isTrue(),
  800. 'Line:'.__LINE__.' XML string should load successfully.'
  801. );
  802. // Test error handling.
  803. $this->assertThat(
  804. $form->getGroup('bogus'),
  805. $this->equalTo(array()),
  806. 'Line:'.__LINE__.' A group that does not exist should return an empty array.'
  807. );
  808. // Test general usage.
  809. $this->assertThat(
  810. count($form->getGroup('params')),
  811. $this->equalTo(3),
  812. 'Line:'.__LINE__.' The params group should have 3 field elements.'
  813. );
  814. $this->assertThat(
  815. count($form->getGroup('level1', true)),
  816. $this->equalTo(2),
  817. 'Line:'.__LINE__.' The level1 group should have 2 nested field elements.'
  818. );
  819. $this->assertThat(
  820. array_keys($form->getGroup('level1', true)),
  821. $this->equalTo(array('level1_field1', 'level1_level2_field2')),
  822. 'Line:'.__LINE__.' The level1 group should have 2 nested field elements.'
  823. );
  824. $this->assertThat(
  825. count($form->getGroup('level1.level2')),
  826. $this->equalTo(1),
  827. 'Line:'.__LINE__.' The level2 group should have 1 field element.'
  828. );
  829. }
  830. /**
  831. * Test for JForm::getInput method.
  832. */
  833. public function testGetInput()
  834. {
  835. $form = new JFormInspector('form1');
  836. $this->assertThat(
  837. $form->load(JFormDataHelper::$loadFieldDocument),
  838. $this->isTrue(),
  839. 'Line:'.__LINE__.' XML string should load successfully.'
  840. );
  841. $this->assertThat(
  842. $form->getInput('title', null, 'The Title'),
  843. $this->equalTo('<input type="text" name="title" id="title_id" value="The Title" class="inputbox required"/>'),
  844. 'Line:'.__LINE__.' The method should return a simple input text field.'
  845. );
  846. $this->assertThat(
  847. $form->getInput('show_title', 'params', '0'),
  848. $this->equalTo(
  849. '<fieldset id="params_show_title" class="radio">' .
  850. '<input type="radio" id="params_show_title0" name="params[show_title]" value="1"/>' .
  851. '<label for="params_show_title0">'.JText::_('JYes').'</label>' .
  852. '<input type="radio" id="params_show_title1" name="params[show_title]" value="0" checked="checked"/>' .
  853. '<label for="params_show_title1">'.JText::_('JNo').'</label>' .
  854. '</fieldset>'
  855. ),
  856. 'Line:'.__LINE__.' The method should return a radio list.'
  857. );
  858. $form = new JFormInspector('form1', array('control' => 'jform'));
  859. $this->assertThat(
  860. $form->load(JFormDataHelper::$loadFieldDocument),
  861. $this->isTrue(),
  862. 'Line:'.__LINE__.' XML string should load successfully.'
  863. );
  864. $this->assertThat(
  865. $form->getInput('colours', 'params', 'blue'),
  866. $this->equalTo(
  867. '<select id="jform_params_colours" name="jform[params][colours][]" multiple="multiple">' .
  868. "\n".' <option value="red">Red</option>' .
  869. "\n".' <option value="blue" selected="selected">Blue</option>' .
  870. "\n".' <option value="green">Green</option>' .
  871. "\n".' <option value="yellow">Yellow</option>' .
  872. "\n".'</select>'.
  873. "\n"
  874. ),
  875. 'Line:'.__LINE__.' XML string should load successfully.'
  876. );
  877. // Test translate default
  878. $this->assertThat(
  879. $form->getInput('translate_default'),
  880. $this->equalTo(
  881. '<input type="text" name="jform[translate_default]" id="jform_translate_default" value="DEFAULT_KEY"/>'
  882. ),
  883. 'Line:'.__LINE__.' The method should return a simple input text field whose value is untranslated since the DEFAULT_KEY does not exist in the language.'
  884. );
  885. $lang = JFactory::getLanguage();
  886. $debug = $lang->setDebug(true);
  887. $this->assertThat(
  888. $form->getInput('translate_default'),
  889. $this->equalTo(
  890. '<input type="text" name="jform[translate_default]" id="jform_translate_default" value="??DEFAULT_KEY??"/>'
  891. ),
  892. 'Line:'.__LINE__.' The method should return a simple input text field whose value is marked untranslated.'
  893. );
  894. $lang->load('form_test', __DIR__);
  895. $this->assertThat(
  896. $form->getInput('translate_default'),
  897. $this->equalTo(
  898. '<input type="text" name="jform[translate_default]" id="jform_translate_default" value="My Default"/>'
  899. ),
  900. 'Line:'.__LINE__.' The method should return a simple input text field whose value is translated.'
  901. );
  902. $lang->setDebug($debug);
  903. }
  904. /**
  905. * Test for JForm::getLabel method.
  906. */
  907. public function testGetLabel()
  908. {
  909. $form = new JFormInspector('form1');
  910. $this->assertThat(
  911. $form->load(JFormDataHelper::$loadFieldDocument),
  912. $this->isTrue(),
  913. 'Line:'.__LINE__.' XML string should load successfully.'
  914. );
  915. $this->assertThat(
  916. $form->getLabel('title'),
  917. $this->equalTo('<label id="title_id-lbl" for="title_id" class="hasTip required" title="Title::The title.">Title<span class="star">&#160;*</span></label>'),
  918. 'Line:'.__LINE__.' The method should return a simple label field.'
  919. );
  920. }
  921. /**
  922. * Test the JForm::getName method.
  923. */
  924. public function testGetName()
  925. {
  926. $form = new JForm('form1');
  927. $this->assertThat(
  928. $form->getName(),
  929. $this->equalTo('form1'),
  930. 'Line:'.__LINE__.' The form name should agree with the argument passed in the constructor.'
  931. );
  932. }
  933. /**
  934. * Test for JForm::getValue method.
  935. */
  936. public function testGetValue()
  937. {
  938. $form = new JFormInspector('form1');
  939. $this->assertThat(
  940. $form->load(JFormDataHelper::$loadFieldDocument),
  941. $this->isTrue(),
  942. 'Line:'.__LINE__.' XML string should load successfully.'
  943. );
  944. $data = array(
  945. 'title' => 'Avatar',
  946. );
  947. $this->assertThat(
  948. $form->bind($data),
  949. $this->isTrue(),
  950. 'Line:'.__LINE__.' The data should bind successfully.'
  951. );
  952. $this->assertThat(
  953. $form->getValue('title'),
  954. $this->equalTo('Avatar'),
  955. 'Line:'.__LINE__.' The bind value should be returned.'
  956. );
  957. }
  958. /**
  959. * Test for JForm::getFieldset method.
  960. */
  961. public function testGetFieldset()
  962. {
  963. // Prepare the form.
  964. $form = new JFormInspector('form1');
  965. $this->assertThat(
  966. $form->load(JFormDataHelper::$getFieldsetDocument),
  967. $this->isTrue(),
  968. 'Line:'.__LINE__.' XML string should load successfully.'
  969. );
  970. $this->assertThat(
  971. $form->getFieldset('bogus'),
  972. $this->equalTo(array()),
  973. 'Line:'.__LINE__.' A fieldset that does not exist should return an empty array.'
  974. );
  975. $this->assertThat(
  976. count($form->getFieldset('params-basic')),
  977. $this->equalTo(4),
  978. 'Line:'.__LINE__.' There are 3 field elements in a fieldset and 1 field element marked with the fieldset attribute.'
  979. );
  980. }
  981. /**
  982. * Test for JForm::getFieldsets method.
  983. */
  984. public function testGetFieldsets()
  985. {
  986. // Prepare the form.
  987. $form = new JFormInspector('form1');
  988. $this->assertThat(
  989. $form->load(JFormDataHelper::$getFieldsetsDocument),
  990. $this->isTrue(),
  991. 'Line:'.__LINE__.' XML string should load successfully.'
  992. );
  993. $sets = $form->getFieldsets();
  994. $this->assertThat(
  995. count($sets),
  996. $this->equalTo(3),
  997. 'Line:'.__LINE__.' The source data has 3 fieldsets in total.'
  998. );
  999. $this->assertThat(
  1000. $sets['params-advanced']->name,
  1001. $this->equalTo('params-advanced'),
  1002. 'Line:'.__LINE__.' Ensure the fieldset name is correct.'
  1003. );
  1004. $this->assertThat(
  1005. $sets['params-advanced']->label,
  1006. $this->equalTo('Advanced Options'),
  1007. 'Line:'.__LINE__.' Ensure the fieldset label is correct.'
  1008. );
  1009. $this->assertThat(
  1010. $sets['params-advanced']->description,
  1011. $this->equalTo('The advanced options'),
  1012. 'Line:'.__LINE__.' Ensure the fieldset description is correct.'
  1013. );
  1014. // Test loading by group.
  1015. $this->assertThat(
  1016. $form->getFieldsets('bogus'),
  1017. $this->equalTo(array()),
  1018. 'Line:'.__LINE__.' A fieldset that in a group that does not exist should return an empty array.'
  1019. );
  1020. $sets = $form->getFieldsets('details');
  1021. $this->assertThat(
  1022. count($sets),
  1023. $this->equalTo(1),
  1024. 'Line:'.__LINE__.' The details group has one field marked with a fieldset'
  1025. );
  1026. $this->assertThat(
  1027. $sets['params-legacy']->name,
  1028. $this->equalTo('params-legacy'),
  1029. 'Line:'.__LINE__.' Ensure the fieldset name is correct.'
  1030. );
  1031. }
  1032. /**
  1033. * Test the JForm::load method.
  1034. *
  1035. * This method can load an XML data object, or parse an XML string.
  1036. */
  1037. public function testLoad()
  1038. {
  1039. $form = new JFormInspector('form1');
  1040. $this->assertThat(
  1041. $form->load(JFormDataHelper::$loadDocument),
  1042. $this->isTrue(),
  1043. 'Line:'.__LINE__.' XML string should load successfully.'
  1044. );
  1045. $this->assertThat(
  1046. ($form->getXML() instanceof JXMLElement),
  1047. $this->isTrue(),
  1048. 'Line:'.__LINE__.' The internal XML should be a JXMLElement object.'
  1049. );
  1050. // Test replace false.
  1051. $this->assertThat(
  1052. $form->load(JFormDataHelper::$loadMergeDocument, false),
  1053. $this->isTrue(),
  1054. 'Line:'.__LINE__.' XML string should load successfully.'
  1055. );
  1056. $this->assertThat(
  1057. count($form->getXML()->xpath('/form/fields/field')),
  1058. $this->equalTo(4),
  1059. 'Line:'.__LINE__.' There are 2 new ungrouped field and one existing field should merge, resulting in 4 total.'
  1060. );
  1061. // Test replace true (default).
  1062. $form = new JFormInspector('form1');
  1063. $this->assertThat(
  1064. $form->load(JFormDataHelper::$loadDocument),
  1065. $this->isTrue(),
  1066. 'Line:'.__LINE__.' XML string should load successfully.'
  1067. );
  1068. $this->assertThat(
  1069. $form->load(JFormDataHelper::$loadMergeDocument),
  1070. $this->isTrue(),
  1071. 'Line:'.__LINE__.' XML string should load successfully.'
  1072. );
  1073. //$this->_showXml($form);die;
  1074. $this->assertThat(
  1075. count($form->findFieldsByGroup(false)),
  1076. $this->equalTo(6),
  1077. 'Line:'.__LINE__.' There are 2 original ungrouped fields, 1 replaced and 4 new, resulting in 6 total.'
  1078. );
  1079. $this->assertThat(
  1080. count($form->getXML()->xpath('//fields[@name]')),
  1081. $this->equalTo(2),
  1082. 'Line:'.__LINE__.' The XML has 2 fields tags with a name attribute.'
  1083. );
  1084. $this->assertThat(
  1085. count($form->getXML()->xpath('//fields[@name="params"]/field')),
  1086. $this->equalTo(2),
  1087. 'Line:'.__LINE__.' The params fields have been merged ending with 2 elements.'
  1088. );
  1089. $this->assertThat(
  1090. count($form->getXML()->xpath('/form/fields/fields[@name="params"]/field[@name="show_abstract"]')),
  1091. $this->equalTo(1),
  1092. 'Line:'.__LINE__.' The show_title in the params group has been replaced by show_abstract.'
  1093. );
  1094. }
  1095. /**
  1096. * Test the JForm::load method for cases of unexpected or bad input.
  1097. *
  1098. * This method can load an XML data object, or parse an XML string.
  1099. */
  1100. public function testLoad_BadInput()
  1101. {
  1102. $form = new JFormInspector('form1');
  1103. $this->assertThat(
  1104. $form->load(123),
  1105. $this->isFalse(),
  1106. 'Line:'.__LINE__.' A non-string should return false.'
  1107. );
  1108. $this->assertThat(
  1109. $form->load('junk'),
  1110. $this->isFalse(),
  1111. 'Line:'.__LINE__.' An invalid string should return false.'
  1112. );
  1113. $this->assertThat(
  1114. $form->getXml(),
  1115. $this->isNull(),
  1116. 'Line:'.__LINE__.' The internal XML should be false as returned from simplexml_load_string.'
  1117. );
  1118. $this->assertThat(
  1119. $form->load('<notform><test /></notform>'),
  1120. $this->isTrue(),
  1121. 'Line:'.__LINE__.' Invalid root node name from string should still load.'
  1122. );
  1123. $this->assertThat(
  1124. $form->getXml()->getName(),
  1125. $this->equalTo('form'),
  1126. 'Line:'.__LINE__.' The internal XML should still be named "form".'
  1127. );
  1128. // Test for irregular object input.
  1129. $form = new JFormInspector('form1');
  1130. $this->assertThat(
  1131. $form->load(JFactory::getXml('<notform><test /></notform>', false)),
  1132. $this->isTrue(),
  1133. 'Line:'.__LINE__.' Invalid root node name from XML object should still load.'
  1134. );
  1135. $this->assertThat(
  1136. $form->getXml()->getName(),
  1137. $this->equalTo('form'),
  1138. 'Line:'.__LINE__.' The internal XML should still be named "form".'
  1139. );
  1140. }
  1141. /**
  1142. * Test the JForm::load method for XPath data.
  1143. *
  1144. * This method can load an XML data object, or parse an XML string.
  1145. */
  1146. public function testLoad_XPath()
  1147. {
  1148. $form = new JFormInspector('form1');
  1149. $this->assertThat(
  1150. $form->load(JFormDataHelper::$loadXPathDocument, true, '/extension/fields'),
  1151. $this->isTrue(),
  1152. 'Line:'.__LINE__.' XML string should load successfully.'
  1153. );
  1154. $this->assertThat(
  1155. $form->getXml()->getName(),
  1156. $this->equalTo('form'),
  1157. 'Line:'.__LINE__.' The internal XML should still be named "form".'
  1158. );
  1159. //$this->_showXml($form);die;
  1160. $this->assertThat(
  1161. count($form->getXml()->fields->fields),
  1162. $this->equalTo(2),
  1163. 'Line:'.__LINE__.' The test data has 2 fields.'
  1164. );
  1165. }
  1166. /**
  1167. * Test for JForm::loadField method.
  1168. */
  1169. public function testLoadField()
  1170. {
  1171. $form = new JFormInspector('form1');
  1172. $this->assertThat(
  1173. $form->load(JFormDataHelper::$loadFieldDocument),
  1174. $this->isTrue(),
  1175. 'Line:'.__LINE__.' XML string should load successfully.'
  1176. );
  1177. // Error handling.
  1178. $this->assertThat(
  1179. $form->loadField('bogus'),
  1180. $this->isFalse(),
  1181. 'Line:'.__LINE__.' An unknown field should return false.'
  1182. );
  1183. // Test correct usage.
  1184. $field = $form->getField('title');
  1185. $field = $form->loadField($field);
  1186. }
  1187. /**
  1188. * Test the JForm::loadFieldType method.
  1189. */
  1190. public function testLoadFieldType()
  1191. {
  1192. $this->assertThat(
  1193. JFormInspector::loadFieldType('bogus'),
  1194. $this->isFalse(),
  1195. 'Line:'.__LINE__.' loadFieldType should return false if class not found.'
  1196. );
  1197. $this->assertThat(
  1198. (JFormInspector::loadFieldType('list') instanceof JFormFieldList),
  1199. $this->isTrue(),
  1200. 'Line:'.__LINE__.' loadFieldType should return the correct class.'
  1201. );
  1202. // Add custom path.
  1203. JForm::addFieldPath(__DIR__.'/_testfields');
  1204. $this->assertThat(
  1205. (JFormInspector::loadFieldType('test') instanceof JFormFieldTest),
  1206. $this->isTrue(),
  1207. 'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
  1208. );
  1209. $this->assertThat(
  1210. (JFormInspector::loadFieldType('foo.bar') instanceof FooFormFieldBar),
  1211. $this->isTrue(),
  1212. 'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
  1213. );
  1214. $this->assertThat(
  1215. (JFormInspector::loadFieldType('modal_foo') instanceof JFormFieldModal_Foo),
  1216. $this->isTrue(),
  1217. 'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
  1218. );
  1219. $this->assertThat(
  1220. (JFormInspector::loadFieldType('foo.modal_bar') instanceof FooFormFieldModal_Bar),
  1221. $this->isTrue(),
  1222. 'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
  1223. );
  1224. }
  1225. /**
  1226. * Test the JForm::loadFile method.
  1227. *
  1228. * This method loads a file and passes the string to the JForm::load method.
  1229. */
  1230. public function testLoadFile()
  1231. {
  1232. $form = new JFormInspector('form1');
  1233. // Test for files that don't exist.
  1234. $this->assertThat(
  1235. $form->loadFile('/tmp/example.xml'),
  1236. $this->isFalse(),
  1237. 'Line:'.__LINE__.' A file path that does not exist should return false.'
  1238. );
  1239. $this->assertThat(
  1240. $form->loadFile('notfound'),
  1241. $this->isFalse(),
  1242. 'Line:'.__LINE__.' A file name that does not exist should return false.'
  1243. );
  1244. // Testing loading a file by full path.
  1245. $this->assertThat(
  1246. $form->loadFile(__DIR__.'/example.xml'),
  1247. $this->isTrue(),
  1248. 'Line:'.__LINE__.' XML file by full path should load successfully.'
  1249. );
  1250. $this->assertThat(
  1251. ($form->getXML() instanceof JXMLElement),
  1252. $this->isTrue(),
  1253. 'Line:'.__LINE__.' XML string should parse successfully.'
  1254. );
  1255. // Testing loading a file by file name.
  1256. $form = new JFormInspector('form1');
  1257. JForm::addFormPath(__DIR__);
  1258. $this->assertThat(
  1259. $form->loadFile('example'),
  1260. $this->isTrue(),
  1261. 'Line:'.__LINE__.' XML file by name should load successfully.'
  1262. );
  1263. $this->assertThat(
  1264. ($form->getXML() instanceof JXMLElement),
  1265. $this->isTrue(),
  1266. 'Line:'.__LINE__.' XML string should parse successfully.'
  1267. );
  1268. }
  1269. /**
  1270. * Test for JForm::loadRuleType method.
  1271. */
  1272. public function testLoadRuleType()
  1273. {
  1274. $form = new JFormInspector('form1');
  1275. // Test error handling.
  1276. $this->assertThat(
  1277. $form->loadRuleType('bogus'),
  1278. $this->isFalse(),
  1279. 'Line:'.__LINE__.' Loading an unknown rule should return false.'
  1280. );
  1281. // Test loading a custom rule.
  1282. JForm::addRulePath(__DIR__.'/_testrules');
  1283. $this->assertThat(
  1284. ($form->loadRuleType('custom') instanceof JFormRule),
  1285. $this->isTrue(),
  1286. 'Line:'.__LINE__.' Loading a known rule should return a rule object.'
  1287. );
  1288. // Test all the stock rules load.
  1289. $this->assertThat(
  1290. ($form->loadRuleType('boolean') instanceof JFormRule),
  1291. $this->isTrue(),
  1292. 'Line:'.__LINE__.' Loading the boolean rule should return a rule object.'
  1293. );
  1294. $this->assertThat(
  1295. ($form->loadRuleType('email') instanceof JFormRule),
  1296. $this->isTrue(),
  1297. 'Line:'.__LINE__.' Loading the email rule should return a rule object.'
  1298. );
  1299. $this->assertThat(
  1300. ($form->loadRuleType('equals') instanceof JFormRule),
  1301. $this->isTrue(),
  1302. 'Line:'.__LINE__.' Loading the equals rule should return a rule object.'
  1303. );
  1304. $this->assertThat(
  1305. ($form->loadRuleType('rules') instanceof JFormRule),
  1306. $this->isTrue(),
  1307. 'Line:'.__LINE__.' Loading the [access control] rules rule should return a rule object.'
  1308. );
  1309. $this->assertThat(
  1310. ($form->loadRuleType('username') instanceof JFormRule),
  1311. $this->isTrue(),
  1312. 'Line:'.__LINE__.' Loading the username rule should return a rule object.'
  1313. );
  1314. $this->assertThat(
  1315. ($form->loadRuleType('options') instanceof JFormRule),
  1316. $this->isTrue(),
  1317. 'Line:'.__LINE__.' Loading the options rule should return a rule object.'
  1318. );
  1319. $this->assertThat(
  1320. ($form->loadRuleType('color') instanceof JFormRule),
  1321. $this->isTrue(),
  1322. 'Line:'.__LINE__.' Loading the color rule should return a rule object.'
  1323. );
  1324. $this->assertThat(
  1325. ($form->loadRuleType('tel') instanceof JFormRule),
  1326. $this->isTrue(),
  1327. 'Line:'.__LINE__.' Loading the tel rule should return a rule object.'
  1328. );
  1329. }
  1330. /**
  1331. * Test the JForm::mergeNode method.
  1332. */
  1333. public function testMergeNode()
  1334. {
  1335. // The source data.
  1336. $xml1 = simplexml_load_string('<form><field name="foo" /></form>', 'JXMLElement');
  1337. // The new data for adding the field.
  1338. $xml2 = simplexml_load_string('<form><field name="bar" type="text" /></form>', 'JXMLElement');
  1339. if ($xml1 === false || $xml2 === false) {
  1340. $this->fail('Line:'.__LINE__.' Error in text XML data');
  1341. }
  1342. JFormInspector::mergeNode($xml1->field, $xml2->field);
  1343. $fields = $xml1->xpath('field[@name="foo"] | field[@type="text"]');
  1344. $this->assertThat(
  1345. count($fields),
  1346. $this->equalTo(1),
  1347. 'Line:'.__LINE__.' Existing attribute "name" should merge, new attribute "type" added.'
  1348. );
  1349. }
  1350. /**
  1351. * Test the JForm::mergeNode method.
  1352. */
  1353. public function testMergeNodes()
  1354. {
  1355. // The source data.
  1356. $xml1 = simplexml_load_string('<form><fields><field name="foo" /></fields></form>', 'JXMLElement');
  1357. // The new data for adding the field.
  1358. $xml2 = simplexml_load_string('<form><fields><field name="foo" type="text" /><field name="soap" /></fields></form>', 'JXMLElement');
  1359. if ($xml1 === false || $xml2 === false) {
  1360. $this->fail('Line:'.__LINE__.' Error in text XML data');
  1361. }
  1362. JFormInspector::mergeNodes($xml1->fields, $xml2->fields);
  1363. $this->assertThat(
  1364. count($xml1->xpath('fields/field')),
  1365. $this->equalTo(2),
  1366. 'Line:'.__LINE__.' The merge should have two field tags, one existing, one new.'
  1367. );
  1368. $this->assertThat(
  1369. count($xml1->xpath('fields/field[@name="foo"] | fields/field[@type="text"]')),
  1370. $this->equalTo(1),
  1371. 'Line:'.__LINE__.' A field of the same name should merge.'
  1372. );
  1373. $this->assertThat(
  1374. count($xml1->xpath('fields/field[@name="soap"]')),
  1375. $this->equalTo(1),
  1376. 'Line:'.__LINE__.' A new field should be added.'
  1377. );
  1378. }
  1379. /**
  1380. * Test for JForm::removeField method.
  1381. */
  1382. public function testRemoveField()
  1383. {
  1384. $form = new JFormInspector('form1');
  1385. $this->assertThat(
  1386. $form->load(JFormDataHelper::$loadDocument),
  1387. $this->isTrue(),
  1388. 'Line:'.__LINE__.' XML string should load successfully.'
  1389. );
  1390. $this->assertThat(
  1391. $form->removeField('title'),
  1392. $this->isTrue(),
  1393. 'Line:'.__LINE__.' The removeField method should return true.'
  1394. );
  1395. $this->assertThat(
  1396. $form->findField('title'),
  1397. $this->isFalse(),
  1398. 'Line:'.__LINE__.' The field should be removed.'
  1399. );
  1400. $this->assertThat(
  1401. $form->removeField('show_title', 'params'),
  1402. $this->isTrue(),
  1403. 'Line:'.__LINE__.' The removeField method should return true.'
  1404. );
  1405. $this->assertThat(
  1406. $form->findField('show_title', 'params'),
  1407. $this->isFalse(),
  1408. 'Line:'.__LINE__.' The field should be removed.'
  1409. );
  1410. }
  1411. /**
  1412. * Test for JForm::removeGroup method.
  1413. */
  1414. public function testRemoveGroup()
  1415. {
  1416. $form = new JFormInspector('form1');
  1417. $this->assertThat(
  1418. $form->load(JFormDataHelper::$loadDocument),
  1419. $this->isTrue(),
  1420. 'Line:'.__LINE__.' XML string should load successfully.'
  1421. );
  1422. $this->assertThat(
  1423. $form->removeGroup('params'),
  1424. $this->isTrue(),
  1425. 'Line:'.__LINE__.' The removeGroup method should return true.'
  1426. );
  1427. $this->assertThat(
  1428. $form->findGroup('params'),
  1429. $this->equalTo(array()),
  1430. 'Line:'.__LINE__.' The group should be removed, returning an empty array.'
  1431. );
  1432. }
  1433. /**
  1434. * Test for JForm::setField method.
  1435. */
  1436. public function testReset()
  1437. {
  1438. $form = new JFormInspector('form1');
  1439. $this->assertThat(
  1440. $form->load(JFormDataHelper::$loadDocument),
  1441. $this->isTrue(),
  1442. 'Line:'.__LINE__.' XML string should load successfully.'
  1443. );
  1444. $data = array(
  1445. 'title' => 'Joomla Framework',
  1446. 'params' => array(
  1447. 'show_title' => 2
  1448. )
  1449. );
  1450. $this->assertThat(
  1451. $form->bind($data),
  1452. $this->isTrue(),
  1453. 'Line:'.__LINE__.' The data should bind successfully.'
  1454. );
  1455. $this->assertThat(
  1456. $form->getValue('title'),
  1457. $this->equalTo('Joomla Framework'),
  1458. 'Line:'.__LINE__.' Confirm the field value is set.'
  1459. );
  1460. $this->assertThat(
  1461. $form->getValue('show_title', 'params'),
  1462. $this->equalTo(2),
  1463. 'Line:'.__LINE__.' Confirm the field value is set.'
  1464. );
  1465. // Test reset on the data only.
  1466. $this->assertThat(
  1467. $form->reset(),
  1468. $this->isTrue(),
  1469. 'Line:'.__LINE__.' The reset method should return true.'
  1470. );
  1471. $this->assertThat(
  1472. $form->getField('title'),
  1473. $this->logicalNot($this->isFalse()),
  1474. 'Line:'.__LINE__.' The field should still exist.'
  1475. );
  1476. $this->assertThat(
  1477. $form->getValue('title'),
  1478. $this->equalTo(null),
  1479. 'Line:'.__LINE__.' The field value should be reset.'
  1480. );
  1481. $this->assertThat(
  1482. $form->getValue('show_title', 'params'),
  1483. $this->equalTo(null),
  1484. 'Line:'.__LINE__.' The field value should be reset.'
  1485. );
  1486. // Test reset of data and the internal XML.
  1487. $this->assertThat(
  1488. $form->reset(true),
  1489. $this->isTrue(),
  1490. 'Line:'.__LINE__.' The reset method should return true.'
  1491. );
  1492. $this->assertThat(
  1493. $form->getField('title'),
  1494. $this->isFalse(),
  1495. 'Line:'.__LINE__.' The known field should be removed.'
  1496. );
  1497. $this->assertThat(
  1498. $form->findGroup('params'),
  1499. $this->equalTo(array()),
  1500. 'Line:'.__LINE__.' The known group should be removed, returning an empty array.'
  1501. );
  1502. }
  1503. /**
  1504. * Test for JForm::setField method.
  1505. */
  1506. public function testSetField()
  1507. {
  1508. $form = new JFormInspector('form1');
  1509. $this->assertThat(
  1510. $form->load(JFormDataHelper::$loadDocument),
  1511. $this->isTrue(),
  1512. 'Line:'.__LINE__.' XML string should load successfully.'
  1513. );
  1514. $xml1 = simplexml_load_string('<form><field name="title" required="true" /></form>', 'JXMLElement');
  1515. if ($xml1 === false) {
  1516. $this->fail('Error in text XML data');
  1517. }
  1518. // Test without replace.
  1519. $this->assertThat(
  1520. $form->setField($xml1->field[0], null, false),
  1521. $this->isTrue(),
  1522. 'Line:'.__LINE__.' The setField method should return true.'
  1523. );
  1524. $this->assertThat(
  1525. $form->getFieldAttribute('title', 'required', 'default'),
  1526. $this->equalTo('default'),
  1527. 'Line:'.__LINE__.' The label should contain just the field name.'
  1528. );
  1529. // Test with replace.
  1530. $this->assertThat(
  1531. $form->setField($xml1->field[0], null, true),
  1532. $this->isTrue(),
  1533. 'Line:'.__LINE__.' The setField method should return true.'
  1534. );
  1535. $this->assertThat(
  1536. $form->getFieldAttribute('title', 'required', 'default'),
  1537. $this->equalTo('true'),
  1538. 'Line:'.__LINE__.' The label should contain just the new label.'
  1539. );
  1540. }
  1541. /**
  1542. * Test for JForm::setFieldAttribute method.
  1543. */
  1544. public function testSetFieldAttribute()
  1545. {
  1546. $form = new JFormInspector('form1');
  1547. $this->assertThat(
  1548. $form->load(JFormDataHelper::$loadDocument),
  1549. $this->isTrue(),
  1550. 'Line:'.__LINE__.' XML string should load successfully.'
  1551. );
  1552. $this->assertThat(
  1553. $form->setFieldAttribute('title', 'label', 'The Title'),
  1554. $this->isTrue(),
  1555. 'Line:'.__LINE__.' The method should return true.'
  1556. );
  1557. $this->assertThat(
  1558. $form->getFieldAttribute('title', 'label'),
  1559. $this->equalTo('The Title'),
  1560. 'Line:'.__LINE__.' The new value should be set.'
  1561. );
  1562. $this->assertThat(
  1563. $form->setFieldAttribute('show_title', 'label', 'Show Title', 'params'),
  1564. $this->isTrue(),
  1565. 'Line:'.__LINE__.' The method should return true.'
  1566. );
  1567. $this->assertThat(
  1568. $form->getFieldAttribute('show_title', 'label', 'default', 'params'),
  1569. $this->equalTo('Show Title'),
  1570. 'Line:'.__LINE__.' The new value of the grouped field should be set.'
  1571. );
  1572. }
  1573. /**
  1574. * Test for JForm::setFields method.
  1575. */
  1576. public function testSetFields()
  1577. {
  1578. $form = new JFormInspector('form1');
  1579. $this->assertThat(
  1580. $form->load(JFormDataHelper::$loadDocument),
  1581. $this->isTrue(),
  1582. 'Line:'.__LINE__.' XML string should load successfully.'
  1583. );
  1584. $xml1 = simplexml_load_string('<form><field name="title" required="true" /><field name="ordering" /></form>', 'JXMLElement');
  1585. if ($xml1 === false) {
  1586. $this->fail('Error in text XML data');
  1587. }
  1588. // Test without replace.
  1589. $this->assertThat(
  1590. $form->setFields($xml1->field, null, false),
  1591. $this->isTrue(),
  1592. 'Line:'.__LINE__.' The setFields method should return true.'
  1593. );
  1594. $this->assertThat(
  1595. $form->getFieldAttribute('title', 'required', 'default'),
  1596. $this->equalTo('default'),
  1597. 'Line:'.__LINE__.' The label should contain just the field name.'
  1598. );
  1599. $this->assertThat(
  1600. $form->getField('ordering'),
  1601. $this->logicalNot($this->isFalse()),
  1602. 'Line:'.__LINE__.' The label should contain just the field name.'
  1603. );
  1604. }
  1605. /*…

Large files files are truncated, but you can click here to view the full file