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

Language PHP Lines 163
MD5 Hash 1557de9093b62735b26d4fc91dc45fa5
Repository https://github.com/kareypowell/croogo.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php

App::uses('Term', 'Taxonomy.Model');
App::uses('CroogoTestCase', 'Croogo.TestSuite');

class TermTest extends CroogoTestCase {

	public $fixtures = array(
		'plugin.users.aco',
		'plugin.users.aro',
		'plugin.users.aros_aco',
		'plugin.blocks.block',
		'plugin.comments.comment',
		'plugin.contacts.contact',
		'plugin.translate.i18n',
		'plugin.settings.language',
		'plugin.menus.link',
		'plugin.menus.menu',
		'plugin.contacts.message',
		'plugin.meta.meta',
		'plugin.nodes.node',
		'plugin.taxonomy.model_taxonomy',
		'plugin.blocks.region',
		'plugin.users.role',
		'plugin.settings.setting',
		'plugin.taxonomy.taxonomy',
		'plugin.taxonomy.term',
		'plugin.taxonomy.type',
		'plugin.taxonomy.types_vocabulary',
		'plugin.users.user',
		'plugin.taxonomy.vocabulary',
	);

	public function setUp() {
		parent::setUp();
		$this->Term = ClassRegistry::init('Taxonomy.Term');
	}

	public function tearDown() {
		parent::tearDown();
		unset($this->Term);
	}

	public function testSaveAndGetIdShouldNotCreateNewTermWhenSlugAlreadyExists() {
		$oldCount = $this->Term->find('count');
		$exisitingTermData = array(
			'title' => 'Uncategorized',
			'slug' => 'uncategorized',
			'description' => ''
		);

		$this->Term->saveAndGetId($exisitingTermData);
		$newCount = $this->Term->find('count');

		$this->assertEquals($oldCount, $newCount);
	}

	public function testSaveAndGetIdShouldReturnExistingIdOfTermWhenSlugAlreadyExists() {
		$exisitingTermData = array(
			'title' => 'Uncategorized',
			'slug' => 'uncategorized',
			'description' => ''
		);

		$termId = $this->Term->saveAndGetId($exisitingTermData);
		$expectedId = 1;

		$this->assertEquals($expectedId, $termId);
	}

	public function testSaveAndGetIdShouldReturnNewlyCreatedIdOfTermWhenSlugIsNew() {
		$existingIds = $this->Term->find('all', array('fields' => array('id')));

		$newTermData = array(
			'title' => 'Bazinga',
			'slug' => 'bazinga',
			'description' => ''
		);

		$termId = $this->Term->saveAndGetId($newTermData);

		$this->assertFalse(in_array($termId, $existingIds));
	}

	public function testFindByVocabularyWithNoVocabularyIdShouldTriggerError() {
		$this->setExpectedException('PHPUnit_Framework_error');
		$this->Term->find('byVocabulary');
	}

	public function testFindByVocabularyShouldReturnsTermsOfVocabulary() {
		$terms = $this->Term->find('byVocabulary', array('vocabulary_id' => 1));
		$termIds = Hash::extract($terms, '{n}.Term.id');
		sort($termIds);
		$expectedTermIds = array(1, 2);

		$this->assertEquals($expectedTermIds, $termIds);
	}

	public function testTermIsInVocabularyShouldReturnsTrueIfTermAlreadyInVocabulary() {
		$inVocabulary = $this->Term->isInVocabulary(1, 1);
		$this->assertTrue($inVocabulary);
	}

	public function testAddShouldAddNewTerm() {
		$newTermData = array(
			'Taxonomy' => array('parent_id' => null),
			'Term' => array(
				'title' => 'Bazinga',
				'slug' => 'bazinga',
				'description' => ''
			)
		);

		$this->Term->add($newTermData, 1);
		$newTerm = $this->Term->find('first', array('conditions' => array('slug' => 'bazinga')));

		$this->assertNotEmpty($newTerm);
	}

	public function testHasSlugChangedShouldReturnTrueIfSlugChanged() {
		$changed = $this->Term->hasSlugChanged(1, 'drunk-robot');
		$this->assertTrue($changed);
	}

	public function testHasSlugShouldReturnFalseIfSlugStilltheSame() {
		$changed = $this->Term->hasSlugChanged(1, 'uncategorized');
		$this->assertFalse($changed);
	}

	public function testHasSlugChangedShouldThrowExceptionOnInvalidId() {
		$this->setExpectedException('NotFoundException');
		$this->Term->hasSlugChanged('invalid', 'blah');
	}

	public function testEditShouldReturnTrueWhenRecordSaved() {
		$record = $this->Term->find('first', array('conditions' => array('id' => '1')));
		$record['Taxonomy'] = array('id' => 1, 'parent_id' => null);
		$edited = $this->Term->edit($record, 1);
		$this->assertTrue((bool)$edited);
	}

	public function testEditShouldUpdateRecord() {
		$record = $this->Term->find('first', array('conditions' => array('id' => '1')));
		$record['Term']['slug'] = 'drifting-monkey';
		$record['Taxonomy'] = array('id' => 1, 'parent_id' => null);
		$edited = $this->Term->edit($record, 1);

		$newSlug = $this->Term->field('slug', array('id' => 1));
		$expected = 'drifting-monkey';
		$this->assertEquals($expected, $newSlug);
	}

	public function testRemoveShouldDeleteTerm() {
		$oldCount = $this->Term->find('count');

		$this->Term->remove(1, 1);

		$newCount = $this->Term->find('count');
		$this->assertEquals($oldCount - 1, $newCount);
	}

}
Back to Top