PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Menus/Test/Case/Controller/LinksControllerTest.php

https://github.com/kareypowell/croogo
PHP | 324 lines | 251 code | 20 blank | 53 comment | 0 complexity | 1a37bc17dbaca967f72e82af64ef697a MD5 | raw file
  1. <?php
  2. App::uses('LinksController', 'Menus.Controller');
  3. App::uses('CroogoControllerTestCase', 'Croogo.TestSuite');
  4. class LinksControllerTest extends CroogoControllerTestCase {
  5. public $fixtures = array(
  6. 'plugin.users.aco',
  7. 'plugin.users.aro',
  8. 'plugin.users.aros_aco',
  9. 'plugin.blocks.block',
  10. 'plugin.comments.comment',
  11. 'plugin.contacts.contact',
  12. 'plugin.translate.i18n',
  13. 'plugin.settings.language',
  14. 'plugin.contacts.message',
  15. 'plugin.meta.meta',
  16. 'plugin.nodes.node',
  17. 'plugin.taxonomy.model_taxonomy',
  18. 'plugin.blocks.region',
  19. 'plugin.users.role',
  20. 'plugin.settings.setting',
  21. 'plugin.menus.menu',
  22. 'plugin.menus.link',
  23. 'plugin.taxonomy.taxonomy',
  24. 'plugin.taxonomy.term',
  25. 'plugin.taxonomy.type',
  26. 'plugin.taxonomy.types_vocabulary',
  27. 'plugin.users.user',
  28. 'plugin.taxonomy.vocabulary',
  29. );
  30. /**
  31. * setUp
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. parent::setUp();
  37. $this->LinksController = $this->generate('Menus.Links', array(
  38. 'methods' => array(
  39. 'redirect',
  40. ),
  41. 'components' => array(
  42. 'Auth' => array('user'),
  43. 'Session',
  44. ),
  45. ));
  46. $this->LinksController->Auth
  47. ->staticExpects($this->any())
  48. ->method('user')
  49. ->will($this->returnCallback(array($this, 'authUserCallback')));
  50. }
  51. /**
  52. * tearDown
  53. *
  54. * @return void
  55. */
  56. public function tearDown() {
  57. parent::tearDown();
  58. unset($this->LinksController);
  59. }
  60. /**
  61. * checks that we were redirected with menu id
  62. */
  63. protected function _expectsRedirectToMenu($menuId) {
  64. $this->controller->expects($this->once())
  65. ->method('redirect')
  66. ->with(array(
  67. 'action' => 'index',
  68. '?' => array('menu_id' => $menuId),
  69. )
  70. );
  71. }
  72. /**
  73. * testAdminIndex
  74. *
  75. * @return void
  76. */
  77. public function testAdminIndex() {
  78. $this->LinksController
  79. ->expects($this->once())
  80. ->method('redirect')
  81. ->with(
  82. $this->equalTo(array(
  83. 'controller' => 'menus',
  84. 'action' => 'index',
  85. ))
  86. );
  87. $this->testAction('/admin/menus/links/index');
  88. $this->testAction('/admin/menus/links/index/?menu_id=3');
  89. $mainMenu = $this->LinksController->Link->Menu->findByAlias('main');
  90. $this->assertEquals($mainMenu, $this->vars['menu']);
  91. $this->assertNotEmpty($this->vars['linksTree']);
  92. $this->assertNotEmpty($this->vars['linksStatus']);
  93. }
  94. /**
  95. * testAdminAdd
  96. *
  97. * @return void
  98. */
  99. public function testAdminAdd() {
  100. $this->expectFlashAndRedirect('The Link has been saved');
  101. $mainMenu = ClassRegistry::init('Menus.Menu')->findByAlias('main');
  102. $this->_expectsRedirectToMenu($mainMenu['Menu']['id']);
  103. $this->testAction('/admin/menus/links/add', array(
  104. 'data' => array(
  105. 'Link' => array(
  106. 'menu_id' => $mainMenu['Menu']['id'],
  107. 'title' => 'Test link',
  108. 'class' => 'test-link',
  109. 'link' => '#test-link',
  110. 'status' => 1,
  111. ),
  112. 'Role' => array(
  113. 'Role' => array(),
  114. ),
  115. ),
  116. ));
  117. $testLink = $this->LinksController->Link->findByLink('#test-link');
  118. $this->assertEqual($testLink['Link']['title'], 'Test link');
  119. }
  120. /**
  121. * testAdminEdit
  122. *
  123. * @return void
  124. */
  125. public function testAdminEdit() {
  126. $this->expectFlashAndRedirect('The Link has been saved');
  127. $homeLink = $this->LinksController->Link->find('first', array(
  128. 'conditions' => array(
  129. 'Link.title' => 'Home',
  130. 'Link.link' => '/',
  131. ),
  132. ));
  133. $this->_expectsRedirectToMenu($homeLink['Link']['menu_id']);
  134. $this->testAction('/admin/links/edit/' . $homeLink['Link']['id'], array(
  135. 'data' => array(
  136. 'Link' => array(
  137. 'id' => $homeLink['Link']['id'],
  138. 'menu_id' => $homeLink['Link']['menu_id'],
  139. 'title' => 'Home [modified]',
  140. 'link' => '/',
  141. 'status' => 1,
  142. ),
  143. 'Role' => array(
  144. 'Role' => array(),
  145. ),
  146. ),
  147. ));
  148. $result = $this->LinksController->Link->findById($homeLink['Link']['id']);
  149. $this->assertEquals('Home [modified]', $result['Link']['title']);
  150. }
  151. public function testAdminEditShouldReorderLinkWhenChangingLinkMenu() {
  152. $contactLink = $this->LinksController->Link->findById(15);
  153. $this->testAction('/admin/links/edit/' . $contactLink['Link']['id'], array(
  154. 'data' => array(
  155. 'Link' => array_merge($contactLink['Link'], array('menu_id' => 5)),
  156. 'Role' => array('Role' => array()),
  157. ),
  158. ));
  159. $newContactLink = $this->LinksController->Link->findById($contactLink['Link']['id']);
  160. $this->assertNotEquals($contactLink['Link']['lft'], $newContactLink['Link']['lft']);
  161. }
  162. public function testAdminEditShouldReorderOldMenuWhenChangingLinkMenu() {
  163. $homeLink = $this->LinksController->Link->findById(7);
  164. $this->testAction('/admin/links/edit/' . $homeLink['Link']['id'], array(
  165. 'data' => array(
  166. 'Link' => array_merge($homeLink['Link'], array('menu_id' => 5)),
  167. 'Role' => array('Role' => array()),
  168. ),
  169. ));
  170. $newAboutLink = $this->LinksController->Link->findById(8);
  171. $this->assertEquals(1, $newAboutLink['Link']['lft']);
  172. }
  173. /**
  174. * testAdminDelete
  175. *
  176. * @return void
  177. */
  178. public function testAdminDelete() {
  179. $this->expectFlashAndRedirect('Link deleted');
  180. $homeLink = ClassRegistry::init('Menus.Link')->find('first', array(
  181. 'conditions' => array(
  182. 'Link.title' => 'Home',
  183. 'Link.link' => '/',
  184. ),
  185. ));
  186. $this->_expectsRedirectToMenu($homeLink['Link']['menu_id']);
  187. $this->testAction('/admin/menus/links/delete/' . $homeLink['Link']['id']);
  188. $hasAny = $this->LinksController->Link->hasAny(array(
  189. 'Link.title' => 'Home',
  190. 'Link.link' => '/',
  191. ));
  192. $this->assertFalse($hasAny);
  193. }
  194. /**
  195. * testAdminMoveUp
  196. *
  197. * @return void
  198. */
  199. public function testAdminMoveUp() {
  200. $this->expectFlashAndRedirect('Moved up successfully');
  201. $mainMenu = ClassRegistry::init('Menus.Menu')->findByAlias('main');
  202. $aboutLink = ClassRegistry::init('Menus.Link')->find('first', array(
  203. 'conditions' => array(
  204. 'Link.menu_id' => $mainMenu['Menu']['id'],
  205. 'Link.title' => 'About',
  206. 'Link.link' => '/about',
  207. ),
  208. ));
  209. $this->_expectsRedirectToMenu($aboutLink['Link']['menu_id']);
  210. $this->testAction('/admin/menus/links/moveup/' . $aboutLink['Link']['id']);
  211. $list = $this->LinksController->Link->generateTreeList(array(
  212. 'Link.menu_id' => $mainMenu['Menu']['id'],
  213. 'Link.status' => 1,
  214. ));
  215. $linkTitles = array_values($list);
  216. $this->assertEqual($linkTitles, array(
  217. 'About',
  218. 'Home',
  219. 'Contact'
  220. ));
  221. }
  222. /**
  223. * testAdminMoveUpWithSteps
  224. *
  225. * @return void
  226. */
  227. public function testAdminMoveUpWithSteps() {
  228. $this->expectFlashAndRedirect('Moved up successfully');
  229. $mainMenu = ClassRegistry::init('Menus.Menu')->findByAlias('main');
  230. $contactLink = ClassRegistry::init('Menus.Link')->find('first', array(
  231. 'conditions' => array(
  232. 'Link.menu_id' => $mainMenu['Menu']['id'],
  233. 'Link.title' => 'Contact',
  234. ),
  235. ));
  236. $this->_expectsRedirectToMenu($contactLink['Link']['menu_id']);
  237. $this->testAction('/admin/menus/links/moveup/' . $contactLink['Link']['id'] . '/' . 2);
  238. $list = $this->LinksController->Link->generateTreeList(array(
  239. 'Link.menu_id' => $mainMenu['Menu']['id'],
  240. 'Link.status' => 1,
  241. ));
  242. $linkTitles = array_values($list);
  243. $this->assertEqual($linkTitles, array(
  244. 'Contact',
  245. 'Home',
  246. 'About',
  247. ));
  248. }
  249. /**
  250. * testAdminMoveDown
  251. *
  252. * @return void
  253. */
  254. public function testAdminMoveDown() {
  255. $this->expectFlashAndRedirect('Moved down successfully');
  256. $mainMenu = ClassRegistry::init('Menus.Menu')->findByAlias('main');
  257. $aboutLink = ClassRegistry::init('Menus.Link')->find('first', array(
  258. 'conditions' => array(
  259. 'Link.menu_id' => $mainMenu['Menu']['id'],
  260. 'Link.title' => 'About',
  261. 'Link.link' => '/about',
  262. ),
  263. ));
  264. $this->_expectsRedirectToMenu($aboutLink['Link']['menu_id']);
  265. $this->testAction('/admin/menus/links/movedown/' . $aboutLink['Link']['id']);
  266. $list = $this->LinksController->Link->generateTreeList(array(
  267. 'Link.menu_id' => $mainMenu['Menu']['id'],
  268. 'Link.status' => 1,
  269. ));
  270. $linkTitles = array_values($list);
  271. $this->assertEqual($linkTitles, array(
  272. 'Home',
  273. 'Contact',
  274. 'About',
  275. ));
  276. }
  277. /**
  278. * testAdminMoveDownWithSteps
  279. *
  280. * @return void
  281. */
  282. public function testAdminMoveDownWithSteps() {
  283. $this->expectFlashAndRedirect('Moved down successfully');
  284. $mainMenu = ClassRegistry::init('Menus.Menu')->findByAlias('main');
  285. $homeLink = ClassRegistry::init('Menus.Link')->find('first', array(
  286. 'conditions' => array(
  287. 'Link.menu_id' => $mainMenu['Menu']['id'],
  288. 'Link.title' => 'Home',
  289. ),
  290. ));
  291. $this->_expectsRedirectToMenu($homeLink['Link']['menu_id']);
  292. $this->testAction('/admin/menus/links/movedown/' . $homeLink['Link']['id'] . '/' . 2);
  293. $list = $this->LinksController->Link->generateTreeList(array(
  294. 'Link.menu_id' => $mainMenu['Menu']['id'],
  295. 'Link.status' => 1,
  296. ));
  297. $linkTitles = array_values($list);
  298. $this->assertEqual($linkTitles, array(
  299. 'About',
  300. 'Contact',
  301. 'Home',
  302. ));
  303. }
  304. }