PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Nodes/Test/Case/Controller/NodesControllerTest.php

https://github.com/kareypowell/croogo
PHP | 507 lines | 364 code | 42 blank | 101 comment | 2 complexity | 9d2437e86133d50ae95092fc88d83dd2 MD5 | raw file
  1. <?php
  2. App::uses('NodesController', 'Nodes.Controller');
  3. App::uses('CroogoControllerTestCase', 'Croogo.TestSuite');
  4. class TestNodesController extends NodesController {
  5. public $name = 'Nodes';
  6. public $autoRender = false;
  7. public $testView = false;
  8. public $blackholed = false;
  9. public function redirect($url, $status = null, $exit = true) {
  10. $this->redirectUrl = $url;
  11. }
  12. public function render($action = null, $layout = null, $file = null) {
  13. if (!$this->testView) {
  14. $this->renderedAction = $action;
  15. } else {
  16. return parent::render($action, $layout, $file);
  17. }
  18. }
  19. protected function _stop($status = 0) {
  20. $this->stopped = $status;
  21. }
  22. public function viewFallback($views) {
  23. $this->testView = true;
  24. return $this->_viewFallback($views);
  25. }
  26. public function securityError($type) {
  27. $this->blackholed = true;
  28. }
  29. }
  30. class NodesControllerTest extends CroogoControllerTestCase {
  31. public $fixtures = array(
  32. 'plugin.users.aco',
  33. 'plugin.users.aro',
  34. 'plugin.users.aros_aco',
  35. 'plugin.blocks.block',
  36. 'plugin.comments.comment',
  37. 'plugin.contacts.contact',
  38. 'plugin.translate.i18n',
  39. 'plugin.settings.language',
  40. 'plugin.menus.link',
  41. 'plugin.menus.menu',
  42. 'plugin.contacts.message',
  43. 'plugin.meta.meta',
  44. 'plugin.nodes.node',
  45. 'plugin.taxonomy.model_taxonomy',
  46. 'plugin.blocks.region',
  47. 'plugin.users.role',
  48. 'plugin.settings.setting',
  49. 'plugin.taxonomy.taxonomy',
  50. 'plugin.taxonomy.term',
  51. 'plugin.taxonomy.type',
  52. 'plugin.taxonomy.types_vocabulary',
  53. 'plugin.users.user',
  54. 'plugin.taxonomy.vocabulary',
  55. );
  56. /**
  57. * setUp
  58. *
  59. * @return void
  60. */
  61. public function setUp() {
  62. parent::setUp();
  63. $this->NodesController = $this->generate('Nodes.Nodes', array(
  64. 'methods' => array(
  65. 'redirect',
  66. 'is',
  67. ),
  68. 'components' => array(
  69. 'Auth' => array('user'),
  70. 'Session' => array('read', 'setFlash'),
  71. 'Security' => array('blackHole'),
  72. ),
  73. ));
  74. $this->NodesController->Node->Behaviors->detach('Acl');
  75. $this->NodesController->Auth
  76. ->staticExpects($this->any())
  77. ->method('user')
  78. ->will($this->returnCallback(array($this, 'authUserCallback')));
  79. $this->NodesController->Session
  80. ->staticExpects($this->any())
  81. ->method('read')
  82. ->will($this->returnValue(array('id' => 1, 'role_id' => 1)));
  83. $this->NodesController->Security->Session = $this->getMock('CakeSession');
  84. }
  85. /**
  86. * tearDown
  87. *
  88. * @return void
  89. */
  90. public function tearDown() {
  91. parent::tearDown();
  92. unset($this->NodesController);
  93. }
  94. /**
  95. * testAdminIndex
  96. *
  97. * @return void
  98. */
  99. public function testAdminIndex() {
  100. $this->testAction('/admin/nodes/index');
  101. $this->assertNotEmpty($this->vars['nodes']);
  102. $this->assertEquals(3, count($this->vars['nodes']));
  103. $this->assertNotEmpty($this->vars['nodes'][0]['Node']);
  104. $this->assertNotEmpty($this->vars['nodes'][0]['User']);
  105. $this->assertArrayHasKey('CustomFields', $this->vars['nodes'][0]);
  106. }
  107. /**
  108. * testPromotedWithVisibilityRole
  109. *
  110. * @return void
  111. */
  112. public function testPromotedWithVisibilityRole() {
  113. CakeSession::write('Auth.User', array(
  114. 'id' => 1,
  115. 'role_id' => 1,
  116. ));
  117. $this->testAction('/nodes/nodes/promoted');
  118. $this->assertTrue(count($this->vars['nodes']) === 2);
  119. }
  120. /**
  121. * testIndexWithVisibilityRole
  122. *
  123. * @return void
  124. */
  125. public function testIndexWithVisibilityRole() {
  126. CakeSession::write('Auth.User', array(
  127. 'id' => 1,
  128. 'role_id' => 1,
  129. ));
  130. $this->testAction('/nodes/nodes/index/type:page', array(
  131. 'return' => 'vars',
  132. ));
  133. $this->assertTrue(count($this->vars['nodes']) === 2);
  134. }
  135. /**
  136. * testAdminIndexSearch
  137. *
  138. * @return void
  139. */
  140. public function testAdminIndexSearch() {
  141. $this->testAction('/admin/nodes/index?filter=about');
  142. $this->assertEquals(1, count($this->vars['nodes']));
  143. $this->assertEquals(2, $this->vars['nodes'][0]['Node']['id']);
  144. $this->assertArrayHasKey('CustomFields', $this->vars['nodes'][0]);
  145. }
  146. /**
  147. * testAdminIndex - from popups
  148. *
  149. * @return void
  150. */
  151. public function testAdminLinks() {
  152. $this->testAction('/admin/nodes/nodes/index/links:1/filter:about');
  153. $this->assertEquals('admin_popup', $this->controller->View->layout);
  154. $this->assertNotEmpty($this->vars['nodes']);
  155. $about = $this->vars['nodes'][1];
  156. $this->assertNotEmpty($this->vars['nodes'][0]['Node']);
  157. $this->assertEquals('about', $about['Node']['slug']);
  158. $this->assertNotEmpty($about['User']);
  159. $this->assertArrayHasKey('CustomFields', $about);
  160. }
  161. /**
  162. * testAdminAdd
  163. *
  164. * @return void
  165. */
  166. public function testAdminAdd() {
  167. $this->expectFlashAndRedirect('Node has been saved');
  168. $this->testAction('/admin/nodes/nodes/add', array(
  169. 'data' => array(
  170. 'Node' => array(
  171. 'title' => 'New Node',
  172. 'slug' => 'new-node',
  173. 'token_key' => 1,
  174. 'body' => '',
  175. 'created' => '',
  176. ),
  177. 'Role' => array(
  178. 'Role' => array(),
  179. ),
  180. 'TaxonomyData' => array(
  181. 1 => array(1),
  182. ),
  183. ),
  184. ));
  185. $newBlog = $this->NodesController->Node->findBySlug('new-node');
  186. $this->assertEqual($newBlog['Node']['title'], 'New Node');
  187. $this->assertNotEmpty($newBlog['Node']['created']);
  188. $this->assertEqual($newBlog['Node']['type'], 'node');
  189. $this->assertNotEquals('0000-00-00 00:00:00', $newBlog['Node']['created']);
  190. }
  191. /**
  192. * testAdminAddBlog
  193. *
  194. * @return void
  195. */
  196. public function testAdminAddBlog() {
  197. $this->expectFlashAndRedirect('Blog has been saved');
  198. $this->testAction('/admin/nodes/nodes/add/blog', array(
  199. 'data' => array(
  200. 'Node' => array(
  201. 'title' => 'New Blog',
  202. 'slug' => 'new-blog',
  203. 'token_key' => 1,
  204. 'body' => '',
  205. 'created' => '',
  206. ),
  207. 'Role' => array(
  208. 'Role' => array(),
  209. ),
  210. 'TaxonomyData' => array(
  211. 1 => array(1),
  212. ),
  213. ),
  214. ));
  215. $newBlog = $this->NodesController->Node->findBySlug('new-blog');
  216. $this->assertEqual($newBlog['Node']['title'], 'New Blog');
  217. $this->assertNotEmpty($newBlog['Node']['created']);
  218. $this->assertEqual($newBlog['Node']['type'], 'blog');
  219. $this->assertNotEquals('0000-00-00 00:00:00', $newBlog['Node']['created']);
  220. }
  221. /**
  222. * testAdminAddCustomCreated
  223. *
  224. * @return void
  225. */
  226. public function testAdminAddCustomCreated() {
  227. $this->expectFlashAndRedirect('Node has been saved');
  228. $title = 'New Blog (custom created value)';
  229. $slug = 'new-blog-custom-created-value';
  230. $this->testAction('/admin/nodes/nodes/add', array(
  231. 'data' => array(
  232. 'Node' => array(
  233. 'title' => $title,
  234. 'slug' => $slug,
  235. 'type' => 'blog',
  236. 'token_key' => 1,
  237. 'body' => '',
  238. 'created' => '2012-03-24 01:02:03',
  239. ),
  240. 'Role' => array(
  241. 'Role' => array(),
  242. ),
  243. 'TaxonomyData' => array(
  244. 1 => array(1),
  245. ),
  246. ),
  247. ));
  248. $this->NodesController->Node->type = 'blog';
  249. $newBlog = $this->NodesController->Node->findBySlug($slug);
  250. $this->assertEqual($newBlog['Node']['title'], $title);
  251. $this->assertNotEmpty($newBlog['Node']['created'], '2012-03-24 01:02:03');
  252. }
  253. /**
  254. * testAdminProcessWithInvalidAction
  255. *
  256. * @return void
  257. */
  258. public function testAdminProcessWithInvalidAction() {
  259. $this->setExpectedException('InvalidArgumentException');
  260. $this->testAction('/admin/nodes/nodes/process', array(
  261. 'data' => array(
  262. 'Node' => array(
  263. 'action' => 'avadakadavra',
  264. '1' => array('id' => 0),
  265. '2' => array('id' => 1),
  266. ),
  267. ),
  268. ));
  269. }
  270. /**
  271. * testAdminProcessDataFormat
  272. *
  273. * @return void
  274. */
  275. public function testAdminProcessDataFormat() {
  276. $this->testAction('/admin/nodes/nodes/process', array(
  277. 'data' => array(
  278. 'Node' => array(
  279. 'checkAll' => '0',
  280. 'action' => 'unpublish',
  281. '1' => array('id' => 0),
  282. '2' => array('id' => 1),
  283. ),
  284. ),
  285. ));
  286. $Node = $this->NodesController->Node;
  287. $Node->id = 1;
  288. $result = $Node->field('status');
  289. $this->assertEquals('1', $result);
  290. $Node->id = 2;
  291. $result = $Node->field('status');
  292. $this->assertEquals('0', $result);
  293. }
  294. /**
  295. * testAdminEdit
  296. *
  297. * @return void
  298. */
  299. public function testAdminEdit() {
  300. $this->expectFlashAndRedirect('Blog has been saved');
  301. $this->testAction('/admin/nodes/nodes/edit/1', array(
  302. 'data' => array(
  303. 'Node' => array(
  304. 'id' => 1,
  305. 'title' => 'Hello World [modified]',
  306. 'slug' => 'hello-world',
  307. 'type' => 'blog',
  308. 'token_key' => 1,
  309. ),
  310. 'Role' => array(
  311. 'Role' => array(),
  312. ),
  313. 'TaxonomyData' => array(
  314. 1 => array(1),
  315. ),
  316. ),
  317. ));
  318. $result = $this->NodesController->Node->findBySlug('hello-world');
  319. $this->assertEquals('Hello World [modified]', $result['Node']['title']);
  320. }
  321. /**
  322. * testAdminDelete
  323. *
  324. * @return void
  325. */
  326. public function testAdminDelete() {
  327. $this->expectFlashAndRedirect('Node deleted');
  328. $this->NodesController->Security
  329. ->expects($this->never())
  330. ->method('blackHole');
  331. $this->testAction('/admin/nodes/nodes/delete/1');
  332. $hasAny = $this->NodesController->Node->hasAny(array(
  333. 'Node.slug' => 'hello-world',
  334. ));
  335. $this->assertFalse($hasAny);
  336. }
  337. /**
  338. * testBlackholedRequest
  339. *
  340. * @return void
  341. */
  342. public function testBlackholedRequest() {
  343. $request = new CakeRequest('/admin/nodes/nodes/delete/1');
  344. $response = new CakeResponse();
  345. $this->Nodes = new TestNodesController($request, $response);
  346. $this->Nodes->constructClasses();
  347. $this->Nodes->request->params['plugin'] = 'nodes';
  348. $this->Nodes->request->params['controller'] = 'nodes';
  349. $this->Nodes->request->params['action'] = 'admin_delete';
  350. $this->Nodes->request->params['prefix'] = 'admin';
  351. $this->Nodes->request->params['pass'] = array();
  352. $this->Nodes->request->params['named'] = array();
  353. $this->Nodes->startupProcess();
  354. $this->Nodes->Node->Behaviors->detach('Tree');
  355. $this->Nodes->invokeAction($request);
  356. $this->assertTrue($this->Nodes->blackholed);
  357. $hasAny = $this->Nodes->Node->hasAny(array(
  358. 'Node.id' => 1,
  359. ));
  360. $this->assertTrue($hasAny);
  361. }
  362. /**
  363. * testViewFallback
  364. *
  365. * @return void
  366. */
  367. public function testViewFallback() {
  368. App::build(array(
  369. 'View' => array(CakePlugin::path('Croogo') . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'Mytheme' . DS),
  370. ), App::PREPEND);
  371. $request = new CakeRequest('/admin/nodes/nodes/delete/1');
  372. $response = new CakeResponse();
  373. $this->Nodes = new TestNodesController($request, $response);
  374. $this->Nodes->constructClasses();
  375. $this->Nodes->startupProcess();
  376. $this->Nodes->Node->Behaviors->detach('Tree');
  377. $this->Nodes->theme = 'Mytheme';
  378. $this->Nodes->plugin = 'Nodes';
  379. $this->Nodes->viewFallback(array('index_blog'));
  380. $this->assertContains('index_blog', $this->Nodes->view);
  381. $this->assertContains('Mytheme', $this->Nodes->view);
  382. $this->Nodes->viewFallback(array('view_1', 'view_blog'));
  383. $this->assertContains('view_1.ctp', $this->Nodes->view);
  384. $this->assertContains('Mytheme', $this->Nodes->view);
  385. }
  386. /**
  387. * testViewFallback from plugin controller that extends NodesController
  388. *
  389. * @return void
  390. */
  391. public function testViewFallbackInPlugins() {
  392. CakePlugin::load('TestPlugin');
  393. $this->Nodes = $this->getMock('TestNodesController',
  394. array('render'), array(new CakeRequest(), new CakeResponse())
  395. );
  396. $this->Nodes = new TestNodesController(new CakeRequest(), new CakeResponse);
  397. $this->Nodes->constructClasses();
  398. $this->Nodes->startupProcess();
  399. $this->Nodes->theme = null;
  400. $this->Nodes->plugin = 'TestPlugin';
  401. $this->Nodes->viewFallback(array('index_event'));
  402. $this->assertContains('index_event.ctp', $this->Nodes->view);
  403. unset($this->Nodes);
  404. }
  405. /**
  406. * testViewFallback from plugin controller that extends NodesController
  407. * with an active theme
  408. *
  409. * @return void
  410. */
  411. public function testViewFallbackInPluginsWithTheme() {
  412. CakePlugin::load('TestPlugin');
  413. $this->Nodes = $this->getMock('TestNodesController',
  414. null, array(new CakeRequest(), new CakeResponse())
  415. );
  416. $this->Nodes->constructClasses();
  417. $this->Nodes->startupProcess();
  418. $this->Nodes->theme = 'OurTheme';
  419. $this->Nodes->plugin = 'TestPlugin';
  420. $this->Nodes->viewFallback(array('index_event'));
  421. $this->assertContains('index_event.ctp', $this->Nodes->view);
  422. unset($this->Nodes);
  423. }
  424. /**
  425. * testViewFallback correctly use views from Nodes plugin
  426. *
  427. * @return void
  428. */
  429. public function testViewFallbackToCorePlugins() {
  430. CakePlugin::load('TestPlugin');
  431. $this->Nodes = $this->getMock('TestNodesController',
  432. array('render'), array(new CakeRequest(), new CakeResponse())
  433. );
  434. $this->Nodes->theme = null;
  435. $this->Nodes->plugin = 'TestPlugin';
  436. $this->Nodes->constructClasses();
  437. $this->Nodes->startupProcess();
  438. $this->Nodes
  439. ->expects($this->never())
  440. ->method('render');
  441. $this->Nodes->viewFallback(array('view_1', 'view_blog'));
  442. $this->assertNull($this->Nodes->view);
  443. unset($this->Nodes);
  444. }
  445. /**
  446. * testViewFallback for core NodesController with default theme
  447. *
  448. * @return void
  449. */
  450. public function testViewFallbackWithDefaultTheme() {
  451. App::build(array(
  452. 'View' => array(CakePlugin::path('Croogo') . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'Nodes' . DS . 'View' . DS),
  453. ), App::APPEND);
  454. $this->Nodes = $this->getMock('TestNodesController',
  455. array('render'), array(new CakeRequest(), new CakeResponse())
  456. );
  457. $this->Nodes->constructClasses();
  458. $this->Nodes->startupProcess();
  459. $this->Nodes->theme = null;
  460. $this->Nodes->plugin = null;
  461. $this->Nodes->viewFallback(array('index_node'));
  462. $this->assertContains('index_node.ctp', $this->Nodes->view);
  463. unset($this->Nodes);
  464. }
  465. }