PageRenderTime 50ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/test/functional/crud/crudBrowser.class.php

http://selfpublish.googlecode.com/
PHP | 337 lines | 281 code | 39 blank | 17 comment | 8 complexity | 245959b644a840f128793fd923ba21ce MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, AGPL-3.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. class CrudBrowser extends sfTestBrowser
  10. {
  11. protected
  12. $urlPrefix = 'article',
  13. $projectDir = '';
  14. public function setup($options)
  15. {
  16. $this->projectDir = dirname(__FILE__).'/../fixtures';
  17. $this->cleanup();
  18. chdir($this->projectDir);
  19. $task = new sfPropelGenerateModuleTask(new sfEventDispatcher(), new sfFormatter());
  20. $options[] = 'env=test';
  21. $options[] = '--non-verbose-templates';
  22. $task->run(array('crud', 'article', 'Article'), $options);
  23. require_once($this->projectDir.'/config/ProjectConfiguration.class.php');
  24. sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('crud', 'test', true, $this->projectDir));
  25. return $options;
  26. }
  27. public function teardown()
  28. {
  29. $this->cleanup();
  30. return $this;
  31. }
  32. public function browse($options)
  33. {
  34. $options = $this->setup($options);
  35. // list page
  36. $this->
  37. info('list page')->
  38. get('/'.$this->urlPrefix)->
  39. with('request')->begin()->
  40. isParameter('module', $this->urlPrefix)->
  41. isParameter('action', 'index')->
  42. end()->
  43. with('response')->begin()->
  44. isStatusCode(200)->
  45. checkElement('h1', ucfirst($this->urlPrefix).' List')->
  46. checkElement('table thead tr th:nth(0)', 'Id')->
  47. checkElement('table thead tr th:nth(1)', 'Title')->
  48. checkElement('table thead tr th:nth(2)', 'Body')->
  49. checkElement('table thead tr th:nth(3)', 'Online')->
  50. checkElement('table thead tr th:nth(4)', 'Excerpt')->
  51. checkElement('table thead tr th:nth(5)', 'Category')->
  52. checkElement('table thead tr th:nth(6)', 'Created at')->
  53. checkElement('table thead tr th:nth(7)', 'End date')->
  54. checkElement('table thead tr th:nth(8)', 'Book')->
  55. checkElement('table tbody tr td:nth(0)', '1')->
  56. checkElement('table tbody tr td:nth(1)', 'foo title')->
  57. checkElement('table tbody tr td:nth(2)', 'bar body')->
  58. checkElement('table tbody tr td:nth(3)', '1')->
  59. checkElement('table tbody tr td:nth(4)', 'foo excerpt')->
  60. checkElement('table tbody tr td:nth(5)', '1')->
  61. checkElement('table tbody tr td:nth(6)', '/^\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}$/')->
  62. checkElement('table tbody tr td:nth(7)', '')->
  63. checkElement('table tbody tr td:nth(8)', '')->
  64. checkElement(sprintf('a[href*="/%s/new"]', $this->urlPrefix))->
  65. checkElement(sprintf('tbody a[href*="/%s/1%s"]', $this->urlPrefix, in_array('with-show', $options) ? '' : '/edit'))->
  66. checkElement(sprintf('tbody a[href*="/%s/2%s"]', $this->urlPrefix, in_array('with-show', $options) ? '' : '/edit'))->
  67. end()
  68. ;
  69. // create page
  70. $this->
  71. info('create page')->
  72. click('New')->
  73. with('request')->begin()->
  74. isParameter('module', $this->urlPrefix)->
  75. isParameter('action', 'new')->
  76. isParameter('id', null)->
  77. end()->
  78. with('response')->begin()->
  79. isStatusCode(200)->
  80. checkElement('h1', 'New '.ucfirst($this->urlPrefix))->
  81. checkElement(sprintf('a[href*="/%s"]', $this->urlPrefix), 'Cancel')->
  82. checkElement(sprintf('a[href*="/%s/"]', $this->urlPrefix), false)->
  83. end()->
  84. checkFormValues(array(
  85. 'title' => '',
  86. 'body' => '',
  87. 'online' => false,
  88. 'category_id' => 0,
  89. 'end_date' => array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''),
  90. 'book_id' => 0,
  91. 'author_article_list' => array(),
  92. ))
  93. ;
  94. // save
  95. $this->
  96. info('save')->
  97. saveValues($options, array(
  98. 'title' => 'my real title',
  99. 'body' => 'my real body',
  100. 'online' => true,
  101. 'category_id' => 2,
  102. 'end_date' => array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''),
  103. 'book_id' => null,
  104. 'author_article_list' => array(1, 2),
  105. ), 3, true)
  106. ;
  107. // go back to the list
  108. $this->
  109. info('go back to the list')->
  110. click('Cancel')->
  111. isStatusCode(200)->
  112. isRequestParameter('module', $this->urlPrefix)->
  113. isRequestParameter('action', 'index')
  114. ;
  115. // edit page
  116. $this->info('edit page');
  117. if (!in_array('with-show', $options) && ($options['with-show'] === true))
  118. {
  119. $this->click('3');
  120. }
  121. else
  122. {
  123. $this->get(sprintf('/%s/3/edit', $this->urlPrefix));
  124. }
  125. $this->
  126. with('request')->begin()->
  127. isParameter('module', $this->urlPrefix)->
  128. isParameter('action', 'edit')->
  129. isParameter('id', 3)->
  130. end()->
  131. with('response')->begin()->
  132. isStatusCode(200)->
  133. checkElement('h1', 'Edit '.ucfirst($this->urlPrefix))->
  134. checkElement(sprintf('a[href*="/%s"]', $this->urlPrefix), 'Cancel')->
  135. checkElement(sprintf('a[href*="/%s/3"]', $this->urlPrefix), 'Delete')->
  136. checkElement(sprintf('a[href*="/%s/3"][onclick*="confirm"]', $this->urlPrefix))->
  137. checkElement('table tbody th:nth(0)', 'Title')->
  138. checkElement('table tbody th:nth(1)', 'Body')->
  139. checkElement('table tbody th:nth(2)', 'Online')->
  140. checkElement('table tbody th:nth(3)', 'Excerpt')->
  141. checkElement('table tbody th:nth(4)', 'Category id')->
  142. checkElement('table tbody th:nth(5)', 'Created at')->
  143. checkElement('table tbody th:nth(6)', 'End date')->
  144. checkElement('table tbody th:nth(7)', 'Book id')->
  145. checkElement('table tbody th:nth(8)', 'Author article list')->
  146. checkElement('table tbody td select[id="article_category_id"][name="article[category_id]"] option', 2)->
  147. checkElement('table tbody td select[id="article_book_id"][name="article[book_id]"] option', 2)->
  148. end()
  149. ;
  150. // save / validation
  151. $values = array(
  152. 'id' => 1009299,
  153. 'title' => '',
  154. 'body' => 'my body',
  155. 'online' => true,
  156. 'excerpt' => 'my excerpt',
  157. 'category_id' => null,
  158. 'end_date' => array('year' => 0, 'month' => 0, 'day' => 15, 'hour' => '10', 'minute' => '20'),
  159. 'book_id' => 149999,
  160. 'author_article_list' => array(0, 5),
  161. );
  162. $this->
  163. info('save / validation')->
  164. click('Save', array('article' => $values))->
  165. isStatusCode(200)->
  166. isRequestParameter('module', $this->urlPrefix)->
  167. isRequestParameter('action', 'update')->
  168. checkFormValues(array_merge($values, array(
  169. 'end_date' => array('year' => null, 'month' => null, 'day' => 15, 'hour' => '10', 'minute' => '20')))
  170. )->
  171. checkResponseElement('ul[class="error_list"] li:contains("Required.")', 2)->
  172. checkResponseElement('ul[class="error_list"] li:contains("Invalid.")', 4)
  173. ;
  174. // save
  175. $this->
  176. info('save')->
  177. saveValues($options, array(
  178. 'id' => 3,
  179. 'title' => 'my title',
  180. 'body' => 'my body',
  181. 'online' => false,
  182. 'category_id' => 1,
  183. 'end_date' => array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => '10', 'minute' => '20'),
  184. 'book_id' => 1,
  185. 'author_article_list' => array(1, 3),
  186. ), 3, false)
  187. ;
  188. // go back to the list
  189. $this->
  190. info('go back to the list')->
  191. click('Cancel')->
  192. isStatusCode(200)->
  193. isRequestParameter('module', $this->urlPrefix)->
  194. isRequestParameter('action', 'index')
  195. ;
  196. // delete
  197. $this->
  198. info('delete')->
  199. get(sprintf('/%s/3/edit', $this->urlPrefix))->
  200. click('Delete', array(), array('method' => 'delete', '_with_csrf' => true))->
  201. isStatusCode(302)->
  202. isRequestParameter('module', $this->urlPrefix)->
  203. isRequestParameter('action', 'delete')->
  204. isRedirected()->
  205. followRedirect()->
  206. isStatusCode(200)->
  207. isRequestParameter('module', $this->urlPrefix)->
  208. isRequestParameter('action', 'index')->
  209. get(sprintf('/%s/3/edit', $this->urlPrefix))->
  210. isStatusCode(404)
  211. ;
  212. if (in_array('with-show', $options))
  213. {
  214. // show page
  215. $this->
  216. info('show page')->
  217. get(sprintf('/%s/2', $this->urlPrefix))->
  218. with('request')->begin()->
  219. isParameter('module', $this->urlPrefix)->
  220. isParameter('action', 'show')->
  221. isParameter('id', 2)->
  222. end()->
  223. with('response')->begin()->
  224. isStatusCode(200)->
  225. checkElement(sprintf('a[href*="/%s/2%s"]', $this->urlPrefix, in_array('with-show', $options) ? '' : '/edit'), 'Edit')->
  226. checkElement(sprintf('a[href*="/%s"]', $this->urlPrefix), 'List', array('position' => 1))->
  227. checkElement('body table tbody tr:nth(0)', '/Id\:\s+2/')->
  228. checkElement('body table tbody tr:nth(1)', '/Title\:\s+foo foo title/')->
  229. checkElement('body table tbody tr:nth(2)', '/Body\:\s+bar bar body/')->
  230. checkElement('body table tbody tr:nth(3)', '/Online\:\s+/')->
  231. checkElement('body table tbody tr:nth(4)', '/Excerpt\:\s+foo excerpt/')->
  232. checkElement('body table tbody tr:nth(5)', '/Category\:\s+2/')->
  233. checkElement('body table tbody tr:nth(6)', '/Created at\:\s+[0-9\-\:\s]+/')->
  234. checkElement('body table tbody tr:nth(7)', '/End date\:\s+[0-9\-\:\s]+/')->
  235. checkElement('body table tbody tr:nth(8)', '/Book\:\s+/')->
  236. end()
  237. ;
  238. }
  239. else
  240. {
  241. $this->get(sprintf('/%s/show/id/2', $this->urlPrefix))->isStatusCode(404);
  242. }
  243. $this->teardown();
  244. return $this;
  245. }
  246. public function saveValues($options, $values, $id, $creation)
  247. {
  248. $this->
  249. click('Save', array('article' => $values))->
  250. isRedirected()->
  251. isRequestParameter('module', $this->urlPrefix)->
  252. isRequestParameter('action', $creation ? 'create' : 'update')
  253. ;
  254. $this->
  255. followRedirect()->
  256. isStatusCode(200)->
  257. isRequestParameter('module', $this->urlPrefix)->
  258. isRequestParameter('action', 'edit')->
  259. isRequestParameter('id', $id)->
  260. checkFormValues($values)
  261. ;
  262. return $this;
  263. }
  264. public function checkFormValues(array $values)
  265. {
  266. return $this->with('response')->begin()->
  267. checkElement(sprintf('table tbody td input[id="article_title"][name="article[title]"][value="%s"]', $values['title']))->
  268. checkElement('table tbody td textarea[id="article_body"][name="article[body]"]', $values['body'])->
  269. checkElement(sprintf('table tbody td input[id="article_online"][name="article[online]"][type="checkbox"]%s', $values['online'] ? '[checked="checked"]' : ''))->
  270. checkElement(sprintf('table tbody td select[id="article_category_id"][name="article[category_id]"] option[value="1"]%s', $values['category_id'] == 1 ? '[selected="selected"]' : ''), 'Category 1')->
  271. checkElement(sprintf('table tbody td select[id="article_category_id"][name="article[category_id]"] option[value="2"]%s', $values['category_id'] == 2 ? '[selected="selected"]' : ''), 'Category 2')->
  272. checkElement(sprintf('table tbody td select[id="article_book_id"][name="article[book_id]"] option[value=""]%s', $values['book_id'] == '' ? '[selected="selected"]' : ''), '')->
  273. checkElement(sprintf('table tbody td select[id="article_book_id"][name="article[book_id]"] option[value="1"]%s', $values['book_id'] == 1 ? '[selected="selected"]' : ''), 'The definitive guide to symfony')->
  274. checkElement(sprintf('table tbody td select[id="article_author_article_list"][name="article[author_article_list][]"] option[value="1"]%s', in_array(1, $values['author_article_list']) ? '[selected="selected"]' : ''), 'Fabien')->
  275. checkElement(sprintf('table tbody td select[id="article_author_article_list"][name="article[author_article_list][]"] option[value="2"]%s', in_array(2, $values['author_article_list']) ? '[selected="selected"]' : ''), 'Thomas')->
  276. checkElement(sprintf('table tbody td select[id="article_author_article_list"][name="article[author_article_list][]"] option[value="3"]%s', in_array(3, $values['author_article_list']) ? '[selected="selected"]' : ''), 'H?Šl?¨ne')->
  277. checkElement('table tbody td select[id="article_end_date_year"][name="article[end_date][year]"] option[selected="selected"]', (string) $values['end_date']['year'])->
  278. checkElement('table tbody td select[id="article_end_date_month"][name="article[end_date][month]"] option[selected="selected"]', (string) $values['end_date']['month'])->
  279. checkElement('table tbody td select[id="article_end_date_day"][name="article[end_date][day]"] option[selected="selected"]', (string) $values['end_date']['day'])->
  280. checkElement('table tbody td select[id="article_end_date_hour"][name="article[end_date][hour]"] option[selected="selected"]', (string) $values['end_date']['hour'])->
  281. checkElement('table tbody td select[id="article_end_date_minute"][name="article[end_date][minute]"] option[selected="selected"]', (string) $values['end_date']['minute'])->
  282. end();
  283. }
  284. protected function clearDirectory($dir)
  285. {
  286. sfToolkit::clearDirectory($dir);
  287. if (is_dir($dir))
  288. {
  289. rmdir($dir);
  290. }
  291. }
  292. protected function cleanup()
  293. {
  294. $this->clearDirectory(sprintf($this->projectDir.'/apps/crud/modules/%s', $this->urlPrefix));
  295. $this->clearDirectory(sprintf($this->projectDir.'/cache/crud/test/modules/auto%s', ucfirst($this->urlPrefix)));
  296. $this->clearDirectory($this->projectDir.'/test/functional/crud');
  297. }
  298. }