-
Notifications
You must be signed in to change notification settings - Fork 36
/
TaggableBehaviorTest.php
188 lines (162 loc) · 4.51 KB
/
TaggableBehaviorTest.php
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* CakePHP Tags Plugin
*
* Copyright 2009 - 2010, Cake Development Corporation
* 1785 E. Sahara Avenue, Suite 490-423
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright 2009 - 2010, Cake Development Corporation (http://cakedc.com)
* @link http://github.com/CakeDC/Tags
* @package plugins.tags
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Short description for class.
*
* @package plugins.tags
* @subpackage plugins.tags.tests.cases.behaviors
*/
App::uses('Model', 'Model');
class Article extends AppModel {
public $useTable = 'articles';
public $belongsTo = array();
public $hasAndBelongsToMany = array();
public $hasMany = array();
public $hasOne = array();
public $actsAs = array(
'Contents.Taggable',
'Containable'
);
}
class TaggableBehaviorTest extends CakeTestCase {
/**
* Fixtures associated with this test case
*
* @var array
* @return void
* @access public
*/
public $fixtures = array(
'plugin.installer.plugin',
'plugin.contents.global_tagged',
'plugin.contents.global_tag',
'plugin.contents.content_article'
);
/**
* Method executed before each test
*
* @return void
* @access public
*/
public function startTest() {
$this->Article = ClassRegistry::init('Article');
}
/**
* Method executed after each test
*
* @return void
* @access public
*/
public function endTest() {
unset($this->Article);
ClassRegistry::flush();
}
/**
* Testings saving of tags trough the specified field in the tagable model
*
* @return void
* @access public
*/
public function testTagSaving() {
$data['id'] = 1;
$data['tags'] = 'foo, bar, test';
$this->Article->save($data, false);
$result = $this->Article->find('first', array(
'conditions' => array('id' => 1),
'contain' => array('GlobalTag')
));
$this->assertNotEmpty($result['Article']['tags']);
$data['tags'] = 'foo, developer, developer, php';
$this->Article->save($data, false);
$result = $this->Article->find('first', array(
'contain' => array('GlobalTag'),
'conditions' => array(
'id' => 1)));
$this->assertNotEmpty($result['Article']['tags']);
$data['tags'] = 'cakephp:foo, developer, cakephp:developer, cakephp:php';
$this->Article->save($data, false);
$result = $this->Article->GlobalTag->find('all', array(
'recursive' => -1,
'order' => 'GlobalTag.name ASC',
'conditions' => array(
'GlobalTag.identifier' => 'cakephp')));
$result = Set::extract($result, '{n}.GlobalTag.keyname');
$expected = array('developer', 'foo', 'php');
$this->assertEquals($expected, $result);
$result = $this->Article->saveTags('foo, bar', null);
$this->assertFalse($result);
$result = $this->Article->saveTags(array('foo', 'bar'), 'something');
$this->assertFalse($result);
}
/**
* Testings Taggable::tagArrayToString()
*
* @return void
* @access public
*/
public function testTagArrayToString() {
$data['id'] = 1;
$data['tags'] = 'foo, bar, test';
$this->Article->save($data, false);
$result = $this->Article->find('first', array(
'contain' => array('GlobalTag'),
'conditions' => array(
'id' => 1)));
$result = $this->Article->tagArrayToString($result['GlobalTag']);
$this->assertNotEmpty($result);
$this->assertInternalType('string', $result);
$result = $this->Article->tagArrayToString();
$this->assertEmpty($result);
$this->assertInternalType('string', $result);
}
/**
* Testings Taggable::multibyteKey()
*
* @return void
* @access public
*/
public function testMultibyteKey() {
$result = $this->Article->multibyteKey('this is _ a Nice ! - _ key!');
$expected = 'thisisanicekey';
$this->assertEquals($expected, $result);
$result = $this->Article->multibyteKey('Äü-Ü_ß');
$expected = 'äüüß';
//$this->assertEquals($expected, $result); // @todo fix
}
/**
* testAfterFind callback method
*
* @return void
* @access public
*/
public function testAfterFind() {
$data['id'] = 1;
$data['tags'] = 'foo, bar, test';
$this->Article->save($data, false);
$result = $this->Article->find('first', array(
'conditions' => array('id' => 1),
'contain' => array('GlobalTag')
));
$this->assertTrue(isset($result['GlobalTag']));
$this->Article->Behaviors->Taggable->settings['Article']['unsetInAfterFind'] = true;
$result = $this->Article->find('first', array(
'conditions' => array('id' => 1),
'contain' => array('GlobalTag')
));
$this->assertFalse(isset($result['GlobalTag']));
}
}