PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Taxonomy/Test/Case/Model/TermTest.php

https://github.com/kareypowell/croogo
PHP | 162 lines | 130 code | 32 blank | 0 comment | 0 complexity | 1557de9093b62735b26d4fc91dc45fa5 MD5 | raw file
  1. <?php
  2. App::uses('Term', 'Taxonomy.Model');
  3. App::uses('CroogoTestCase', 'Croogo.TestSuite');
  4. class TermTest extends CroogoTestCase {
  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.menus.link',
  15. 'plugin.menus.menu',
  16. 'plugin.contacts.message',
  17. 'plugin.meta.meta',
  18. 'plugin.nodes.node',
  19. 'plugin.taxonomy.model_taxonomy',
  20. 'plugin.blocks.region',
  21. 'plugin.users.role',
  22. 'plugin.settings.setting',
  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. public function setUp() {
  31. parent::setUp();
  32. $this->Term = ClassRegistry::init('Taxonomy.Term');
  33. }
  34. public function tearDown() {
  35. parent::tearDown();
  36. unset($this->Term);
  37. }
  38. public function testSaveAndGetIdShouldNotCreateNewTermWhenSlugAlreadyExists() {
  39. $oldCount = $this->Term->find('count');
  40. $exisitingTermData = array(
  41. 'title' => 'Uncategorized',
  42. 'slug' => 'uncategorized',
  43. 'description' => ''
  44. );
  45. $this->Term->saveAndGetId($exisitingTermData);
  46. $newCount = $this->Term->find('count');
  47. $this->assertEquals($oldCount, $newCount);
  48. }
  49. public function testSaveAndGetIdShouldReturnExistingIdOfTermWhenSlugAlreadyExists() {
  50. $exisitingTermData = array(
  51. 'title' => 'Uncategorized',
  52. 'slug' => 'uncategorized',
  53. 'description' => ''
  54. );
  55. $termId = $this->Term->saveAndGetId($exisitingTermData);
  56. $expectedId = 1;
  57. $this->assertEquals($expectedId, $termId);
  58. }
  59. public function testSaveAndGetIdShouldReturnNewlyCreatedIdOfTermWhenSlugIsNew() {
  60. $existingIds = $this->Term->find('all', array('fields' => array('id')));
  61. $newTermData = array(
  62. 'title' => 'Bazinga',
  63. 'slug' => 'bazinga',
  64. 'description' => ''
  65. );
  66. $termId = $this->Term->saveAndGetId($newTermData);
  67. $this->assertFalse(in_array($termId, $existingIds));
  68. }
  69. public function testFindByVocabularyWithNoVocabularyIdShouldTriggerError() {
  70. $this->setExpectedException('PHPUnit_Framework_error');
  71. $this->Term->find('byVocabulary');
  72. }
  73. public function testFindByVocabularyShouldReturnsTermsOfVocabulary() {
  74. $terms = $this->Term->find('byVocabulary', array('vocabulary_id' => 1));
  75. $termIds = Hash::extract($terms, '{n}.Term.id');
  76. sort($termIds);
  77. $expectedTermIds = array(1, 2);
  78. $this->assertEquals($expectedTermIds, $termIds);
  79. }
  80. public function testTermIsInVocabularyShouldReturnsTrueIfTermAlreadyInVocabulary() {
  81. $inVocabulary = $this->Term->isInVocabulary(1, 1);
  82. $this->assertTrue($inVocabulary);
  83. }
  84. public function testAddShouldAddNewTerm() {
  85. $newTermData = array(
  86. 'Taxonomy' => array('parent_id' => null),
  87. 'Term' => array(
  88. 'title' => 'Bazinga',
  89. 'slug' => 'bazinga',
  90. 'description' => ''
  91. )
  92. );
  93. $this->Term->add($newTermData, 1);
  94. $newTerm = $this->Term->find('first', array('conditions' => array('slug' => 'bazinga')));
  95. $this->assertNotEmpty($newTerm);
  96. }
  97. public function testHasSlugChangedShouldReturnTrueIfSlugChanged() {
  98. $changed = $this->Term->hasSlugChanged(1, 'drunk-robot');
  99. $this->assertTrue($changed);
  100. }
  101. public function testHasSlugShouldReturnFalseIfSlugStilltheSame() {
  102. $changed = $this->Term->hasSlugChanged(1, 'uncategorized');
  103. $this->assertFalse($changed);
  104. }
  105. public function testHasSlugChangedShouldThrowExceptionOnInvalidId() {
  106. $this->setExpectedException('NotFoundException');
  107. $this->Term->hasSlugChanged('invalid', 'blah');
  108. }
  109. public function testEditShouldReturnTrueWhenRecordSaved() {
  110. $record = $this->Term->find('first', array('conditions' => array('id' => '1')));
  111. $record['Taxonomy'] = array('id' => 1, 'parent_id' => null);
  112. $edited = $this->Term->edit($record, 1);
  113. $this->assertTrue((bool)$edited);
  114. }
  115. public function testEditShouldUpdateRecord() {
  116. $record = $this->Term->find('first', array('conditions' => array('id' => '1')));
  117. $record['Term']['slug'] = 'drifting-monkey';
  118. $record['Taxonomy'] = array('id' => 1, 'parent_id' => null);
  119. $edited = $this->Term->edit($record, 1);
  120. $newSlug = $this->Term->field('slug', array('id' => 1));
  121. $expected = 'drifting-monkey';
  122. $this->assertEquals($expected, $newSlug);
  123. }
  124. public function testRemoveShouldDeleteTerm() {
  125. $oldCount = $this->Term->find('count');
  126. $this->Term->remove(1, 1);
  127. $newCount = $this->Term->find('count');
  128. $this->assertEquals($oldCount - 1, $newCount);
  129. }
  130. }