PageRenderTime 112ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 1ms

/Nodes/Test/Case/Model/NodeTest.php

https://github.com/kareypowell/croogo
PHP | 567 lines | 415 code | 74 blank | 78 comment | 0 complexity | 1a4646579fb3577a86ff677607bf2fd7 MD5 | raw file
  1. <?php
  2. App::uses('Node', 'Nodes.Model');
  3. App::uses('CroogoTestCase', 'Croogo.TestSuite');
  4. class NodeTest extends CroogoTestCase {
  5. public $testBody = 'body set from event';
  6. public $fixtures = array(
  7. 'plugin.users.aco',
  8. 'plugin.users.aro',
  9. 'plugin.users.aros_aco',
  10. 'plugin.blocks.block',
  11. 'plugin.comments.comment',
  12. 'plugin.contacts.contact',
  13. 'plugin.translate.i18n',
  14. 'plugin.settings.language',
  15. 'plugin.menus.link',
  16. 'plugin.menus.menu',
  17. 'plugin.contacts.message',
  18. 'plugin.meta.meta',
  19. 'plugin.nodes.node',
  20. 'plugin.taxonomy.model_taxonomy',
  21. 'plugin.blocks.region',
  22. 'plugin.users.role',
  23. 'plugin.settings.setting',
  24. 'plugin.taxonomy.taxonomy',
  25. 'plugin.taxonomy.term',
  26. 'plugin.taxonomy.type',
  27. 'plugin.taxonomy.types_vocabulary',
  28. 'plugin.users.user',
  29. 'plugin.taxonomy.vocabulary',
  30. );
  31. public function setUp() {
  32. parent::setUp();
  33. $this->Node = ClassRegistry::init('Nodes.Node');
  34. $this->Node->Behaviors->unload('Acl');
  35. }
  36. public function tearDown() {
  37. parent::tearDown();
  38. unset($this->Node);
  39. }
  40. /**
  41. * Test before Callbacks.
  42. */
  43. public function testBeforeSave() {
  44. $this->Node->type = 'whut ?';
  45. $data = array(
  46. 'user_id' => 42,
  47. 'title' => 'Test Content',
  48. 'slug' => 'test-content',
  49. 'token_key' => 1,
  50. 'body' => '',
  51. 'path' => '/no-way'
  52. );
  53. $result = $this->Node->save($data);
  54. $this->assertTrue((bool)$result);
  55. $this->assertEquals('whut ?', $result['Node']['type']);
  56. }
  57. public function testBeforeFind() {
  58. $this->Node->type = 'blog';
  59. $node = $this->Node->find('first', array('conditions' => array('DATE(created)' => '2009-12-25'), 'recursive' => -1));
  60. $this->assertNotEmpty($node);
  61. $expectedNodeId = 1;
  62. $this->assertEquals($expectedNodeId, $node['Node']['id']);
  63. $this->assertEquals('blog', $node['Node']['type']);
  64. }
  65. public function testCacheTerms() {
  66. $this->Node->data = array(
  67. 'Node' => array(),
  68. 'Taxonomy' => array(
  69. 'Taxonomy' => array(1, 2), // uncategorized, and announcements
  70. ),
  71. );
  72. $this->Node->cacheTerms();
  73. $terms = json_decode($this->Node->data['Node']['terms'], true);
  74. ksort($terms, SORT_NUMERIC);
  75. $result = json_encode($terms);
  76. $expected = '{"1":"uncategorized","2":"announcements"}';
  77. $this->assertEquals($expected, $result);
  78. }
  79. public function testNodeDeleteDependent() {
  80. // assert existing count
  81. $commentCount = $this->Node->Comment->find('count', array(
  82. 'conditions' => array(
  83. 'Comment.model' => 'Node',
  84. 'Comment.foreign_key' => 1
  85. )
  86. ));
  87. $this->assertEquals(2, $commentCount);
  88. $metaCount = $this->Node->Meta->find('count',
  89. array('conditions' => array('model' => 'Node', 'foreign_key' => 1))
  90. );
  91. $this->assertEquals(1, $metaCount);
  92. // delete node
  93. $this->Node->id = 1;
  94. $this->Node->delete();
  95. $commentCount = $this->Node->Comment->find('count', array(
  96. 'conditions' => array(
  97. 'Comment.model' => 'Node',
  98. 'Comment.foreign_key' => 1,
  99. )
  100. ));
  101. $this->assertEqual(0, $commentCount);
  102. $metaCount = $this->Node->Meta->find('count',
  103. array('conditions' => array('model' => 'Node', 'foreign_key' => 1))
  104. );
  105. $this->assertEqual(0, $metaCount);
  106. }
  107. /**
  108. * test saving node.
  109. */
  110. public function testAddNode() {
  111. $this->Node->Behaviors->disable('Tree');
  112. $this->Node->type = null;
  113. $oldNodeCount = $this->Node->find('count');
  114. $data = array(
  115. 'Node' => array(
  116. 'title' => 'Test Content',
  117. 'slug' => 'test-content',
  118. 'type' => 'blog',
  119. 'token_key' => 1,
  120. 'body' => '',
  121. ),
  122. 'TaxonomyData' => array(
  123. 1 => array(1),
  124. )
  125. );
  126. $result = $this->Node->saveNode($data, Node::DEFAULT_TYPE);
  127. $this->Node->type = null;
  128. $newNodeCount = $this->Node->find('count');
  129. $this->assertTrue($result);
  130. $this->assertTrue($this->Node->Behaviors->enabled('Tree'));
  131. $this->assertEquals($oldNodeCount + 1, $newNodeCount);
  132. }
  133. /**
  134. * testAddNodeWithTaxonomyData
  135. */
  136. public function testAddNodeWithTaxonomyData() {
  137. $this->Node->type = null;
  138. $oldNodeCount = $this->Node->find('count');
  139. $data = array(
  140. 'Node' => array(
  141. 'title' => 'Test Content',
  142. 'slug' => 'test-content',
  143. 'type' => 'blog',
  144. 'token_key' => 1,
  145. 'body' => '',
  146. ),
  147. 'TaxonomyData' => array(1 => array(0 => '1')),
  148. );
  149. $result = $this->Node->saveNode($data, Node::DEFAULT_TYPE);
  150. $this->Node->type = null;
  151. $newNodeCount = $this->Node->find('count');
  152. $this->assertTrue($result);
  153. $this->assertEquals($oldNodeCount + 1, $newNodeCount);
  154. }
  155. /**
  156. * testAddNodeWithTaxonomyMultipleTerms
  157. */
  158. public function testAddNodeWithTaxonomyMultipleTerms() {
  159. $this->Node->type = null;
  160. $data = array(
  161. 'Node' => array(
  162. 'title' => 'Test Content',
  163. 'slug' => 'test-content',
  164. 'type' => 'blog',
  165. 'token_key' => 1,
  166. 'body' => '',
  167. ),
  168. 'TaxonomyData' => array(1 => array(0 => '1', 1 => 2)),
  169. );
  170. $result = $this->Node->saveNode($data, Node::DEFAULT_TYPE);
  171. $this->assertTrue($result);
  172. $this->assertEmpty($this->Node->validationErrors);
  173. $this->Node->type = null;
  174. }
  175. /**
  176. * testAddNodeWithTaxonomyRequiredValidationError
  177. */
  178. public function testAddNodeWithTaxonomyRequiredValidationError() {
  179. $this->Node->type = null;
  180. $data = array(
  181. 'Node' => array(
  182. 'title' => 'Test Content',
  183. 'slug' => 'test-content',
  184. 'type' => 'blog',
  185. 'token_key' => 1,
  186. 'body' => '',
  187. ),
  188. 'TaxonomyData' => array(1 => null),
  189. );
  190. $result = $this->Node->saveNode($data, Node::DEFAULT_TYPE);
  191. $this->assertFalse($result);
  192. $this->assertEquals('Please select at least 1 value', $this->Node->validationErrors['TaxonomyData.1'][0]);
  193. $this->Node->type = null;
  194. }
  195. /**
  196. * testAddNodeWithTaxonomyNonMultipleValidationError
  197. */
  198. public function testAddNodeWithTaxonomyNonMultipleValidationError() {
  199. $this->Node->Taxonomy->Vocabulary->id = 1;
  200. $this->Node->Taxonomy->Vocabulary->saveField('multiple', false);
  201. $this->Node->type = null;
  202. $data = array(
  203. 'Node' => array(
  204. 'title' => 'Test Content',
  205. 'slug' => 'test-content',
  206. 'type' => 'blog',
  207. 'token_key' => 1,
  208. 'body' => '',
  209. ),
  210. 'TaxonomyData' => array(1 => array(0 => '1', 1 => 2)),
  211. );
  212. $result = $this->Node->saveNode($data, Node::DEFAULT_TYPE);
  213. $this->assertFalse($result);
  214. $this->assertEquals('Please select at most 1 value', $this->Node->validationErrors['TaxonomyData.1'][0]);
  215. $this->Node->type = null;
  216. $this->Node->Taxonomy->Vocabulary->id = 1;
  217. $this->Node->Taxonomy->Vocabulary->saveField('multiple', true);
  218. }
  219. /**
  220. * testAddNodeWithVisibilityRole
  221. */
  222. public function testAddNodeWithVisibilityRole() {
  223. $this->Node->type = null;
  224. $oldNodeCount = $this->Node->find('count');
  225. $data = array(
  226. 'Node' => array(
  227. 'title' => 'Test Content',
  228. 'slug' => 'test-content',
  229. 'type' => 'blog',
  230. 'token_key' => 1,
  231. 'body' => '',
  232. ),
  233. 'Role' => array('Role' => array('3')), //Public
  234. 'TaxonomyData' => array(
  235. 1 => array(1),
  236. )
  237. );
  238. $result = $this->Node->saveNode($data, Node::DEFAULT_TYPE);
  239. $this->Node->type = null;
  240. $newNodeCount = $this->Node->find('count');
  241. $this->assertTrue($result);
  242. $this->assertEquals($oldNodeCount + 1, $newNodeCount);
  243. }
  244. /**
  245. * Test onBeforeSaveNode Event Callbacks
  246. */
  247. public function onBeforeSaveNode($Event) {
  248. $Event->data['data']['Node']['body'] = $this->testBody;
  249. }
  250. /**
  251. * Test onAfterSaveNode Event Callbacks
  252. */
  253. public function onAfterSaveNode($Event) {
  254. $this->assertEquals($this->testBody, $Event->data['data']['Node']['body']);
  255. }
  256. /**
  257. * testSaveNodeEvents
  258. */
  259. public function testSaveNodeEvents() {
  260. $this->Node->type = null;
  261. $oldNodeCount = $this->Node->find('count');
  262. $data = array(
  263. 'Node' => array(
  264. 'title' => 'Test Content',
  265. 'slug' => 'test-content',
  266. 'type' => 'blog',
  267. 'token_key' => 1,
  268. 'body' => '',
  269. ),
  270. 'Role' => array('Role' => array('3')), //Public
  271. 'TaxonomyData' => array(
  272. 1 => array(1),
  273. )
  274. );
  275. $manager = CakeEventManager::instance();
  276. $manager->attach(array($this, 'onBeforeSaveNode'), 'Model.Node.beforeSaveNode');
  277. $manager->attach(array($this, 'onAfterSaveNode'), 'Model.Node.afterSaveNode');
  278. $result = $this->Node->saveNode($data, Node::DEFAULT_TYPE);
  279. $this->Node->type = null;
  280. $node = $this->Node->find('first', array(
  281. 'fields' => array('id', 'title', 'slug', 'body'),
  282. 'recursive' => -1,
  283. 'conditions' => array(
  284. 'Node.id' => $this->Node->id,
  285. ),
  286. ));
  287. $this->assertTrue($result);
  288. $this->assertEquals('Test Content', $node['Node']['title']);
  289. $this->assertEquals($this->testBody, $node['Node']['body']);
  290. $manager->detach(array($this, 'onBeforeSaveNode'));
  291. $manager->detach(array($this, 'onAfterSaveNode'));
  292. }
  293. /**
  294. * testAddNodeWithInvalidNodeType
  295. */
  296. public function testAddNodeWithInvalidNodeType() {
  297. $this->setExpectedException('InvalidArgumentException');
  298. $data = array(
  299. 'title' => 'Test Content',
  300. 'slug' => 'test-content',
  301. 'type' => 'invalid',
  302. 'token_key' => 1,
  303. 'body' => '',
  304. );
  305. $result = $this->Node->saveNode($data, 'invalid');
  306. }
  307. /**
  308. * Test filtering methods
  309. */
  310. public function testFilterNodesByTitle() {
  311. $filterConditions = $this->Node->filterNodes(array('filter' => 'Hello'));
  312. $node = $this->Node->find('first', array('conditions' => $filterConditions));
  313. $this->assertNotEmpty($node);
  314. $this->assertEquals(1, $node['Node']['id']);
  315. }
  316. public function testFilterNodesByBody() {
  317. $filterConditions = $this->Node->filterNodes(array('filter' => 'example'));
  318. $node = $this->Node->find('first', array('conditions' => $filterConditions));
  319. $this->assertNotEmpty($node);
  320. $this->assertEquals(2, $node['Node']['id']);
  321. }
  322. public function testFilterNodesWithoutKeyword() {
  323. $filterConditions = $this->Node->filterNodes();
  324. $nodes = $this->Node->find('all', array('conditions' => $filterConditions));
  325. $this->assertEquals(3, count($nodes));
  326. }
  327. /**
  328. * test updateAllNodesPaths
  329. */
  330. public function testUpdateAllNodesPaths() {
  331. $this->Node->id = 1;
  332. $result = $this->Node->saveField('path', 'invalid one');
  333. $this->assertTrue((bool)$result);
  334. CroogoRouter::connect('/blog/:slug', array(
  335. 'plugin' => 'nodes',
  336. 'controller' => 'nodes',
  337. 'action' => 'view',
  338. 'type' => 'blog',
  339. ));
  340. Router::promote();
  341. $result = $this->Node->updateAllNodesPaths();
  342. $this->assertTrue($result);
  343. $this->Node->type = 'blog';
  344. $node = $this->Node->findById(1);
  345. $this->assertEquals('/blog/hello-world', $node['Node']['path']);
  346. }
  347. /**
  348. * Test find('promoted')
  349. */
  350. public function testFindPromoted() {
  351. $results = $this->Node->find('promoted');
  352. $expectedId = 1;
  353. $this->assertEquals(1, count($results));
  354. $this->assertEquals($expectedId, $results[0]['Node']['id']);
  355. $this->assertEquals(Node::STATUS_PUBLISHED, $results[0]['Node']['status']);
  356. $this->assertEquals(Node::STATUS_PROMOTED, $results[0]['Node']['promote']);
  357. }
  358. /**
  359. * test processActionDelete
  360. */
  361. public function testProcessActionDelete() {
  362. $ids = array('1', '2');
  363. $commentCount = $this->Node->Comment->find('count', array(
  364. 'conditions' => array(
  365. 'Comment.model' => 'Node',
  366. 'Comment.foreign_key' => $ids,
  367. )
  368. ));
  369. $this->assertTrue($commentCount > 0);
  370. $success = $this->Node->processAction('delete', $ids);
  371. $count = $this->Node->find('count');
  372. $this->assertTrue($success);
  373. $this->assertEquals(1, $count);
  374. // verifies that related comments are deleted (by afterDelete callback)
  375. $commentCount = $this->Node->Comment->find('count', array(
  376. 'conditions' => array(
  377. 'Comment.model' => 'Node',
  378. 'Comment.foreign_key' => $ids,
  379. )
  380. ));
  381. $this->assertTrue($commentCount === 0);
  382. }
  383. /**
  384. * test processActionPromote
  385. */
  386. public function testProcessActionPromote() {
  387. $ids = array('1', '2');
  388. $success = $this->Node->processAction('promote', $ids);
  389. $newRecords = $this->Node->find('all');
  390. $this->assertTrue($success);
  391. foreach ($newRecords as $record) {
  392. $this->assertTrue($record['Node']['promote']);
  393. }
  394. }
  395. /**
  396. * test processActionUnpromote
  397. */
  398. public function testProcessActionUnpromote() {
  399. $ids = array('1', '2', '3');
  400. $success = $this->Node->processAction('unpromote', $ids);
  401. $newRecords = $this->Node->find('all');
  402. $this->assertTrue($success);
  403. foreach ($newRecords as $record) {
  404. $this->assertFalse($record['Node']['promote']);
  405. }
  406. }
  407. /**
  408. * test processActionPublish
  409. */
  410. public function testProcessActionPublish() {
  411. $ids = array('1', '2');
  412. $success = $this->Node->processAction('publish', $ids);
  413. $newRecords = $this->Node->find('all');
  414. $this->assertTrue($success);
  415. foreach ($newRecords as $record) {
  416. $this->assertEquals(CroogoStatus::PUBLISHED, $record['Node']['status']);
  417. }
  418. }
  419. /**
  420. * test processActionUnpublish
  421. */
  422. public function testProcessActionUnpublish() {
  423. $ids = array('1', '2', '3');
  424. $success = $this->Node->processAction('unpublish', $ids);
  425. $newRecords = $this->Node->find('all');
  426. $this->assertTrue($success);
  427. foreach ($newRecords as $record) {
  428. $this->assertEquals(CroogoStatus::UNPUBLISHED, $record['Node']['status']);
  429. }
  430. }
  431. /**
  432. * test processActionInvalidAction
  433. */
  434. public function testProcessActionInvalidAction() {
  435. $this->setExpectedException('InvalidArgumentException');
  436. $this->Node->processAction('avadakadavra', array(1, 2));
  437. }
  438. /**
  439. * test processActionWithoutIds
  440. */
  441. public function testProcessActionWithoutIds() {
  442. $this->setExpectedException('InvalidArgumentException');
  443. $this->Node->processAction('delete', array());
  444. }
  445. /**
  446. * testFindViewById
  447. */
  448. public function testFindViewById() {
  449. $this->Node->useCache = false;
  450. $node = $this->Node->find('viewById', array(
  451. 'id' => 1,
  452. ));
  453. $this->assertEquals('Hello World', $node['Node']['title']);
  454. }
  455. /**
  456. * testFindViewBySlug
  457. */
  458. public function testFindViewBySlug() {
  459. $this->Node->useCache = false;
  460. $node = $this->Node->find('viewBySlug', array(
  461. 'slug' => 'about',
  462. 'type' => 'page',
  463. ));
  464. $this->assertEquals('About', $node['Node']['title']);
  465. }
  466. /**
  467. * testFindPublish
  468. */
  469. public function testFindPublished() {
  470. $Node = $this->Node;
  471. $Node->useCache = false;
  472. $Node->id = 3;
  473. $Node->saveField('status', 0);
  474. $nodes = $Node->find('published', array(
  475. 'fields' => array('id'),
  476. ));
  477. $extracted = Hash::extract($nodes, '{n}.Node.status');
  478. $this->assertEquals(2, count($nodes));
  479. }
  480. /**
  481. * testFormatDataPreserveSuppliedPath
  482. */
  483. public function testFormatDataPreserveSuppliedPath() {
  484. $Node = $this->Node;
  485. $result = $Node->formatData(array(
  486. 'Node' => array(
  487. 'slug' => 'foo',
  488. 'path' => '/bar/foo',
  489. ),
  490. ));
  491. $this->assertEquals('/bar/foo', $result['Node']['path']);
  492. }
  493. }