1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ValidForeignKey Behavior |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License. |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Marc Würth |
10
|
|
|
* @author Marc Würth <[email protected]> |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
12
|
|
|
* @link https://github.com/ravage84/ValidForeignKeyBehavior |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
App::uses('ValidForeignKeyBehavior', 'ValidForeignKey.Model/Behavior'); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ValidForeignKeyBehaviorTest |
19
|
|
|
* |
20
|
|
|
* @property AppModel $_model |
21
|
|
|
* @coversDefaultClass ValidForeignKeyBehavior |
22
|
|
|
*/ |
23
|
|
|
class ValidForeignKeyBehaviorTest extends CakeTestCase { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Model under test |
27
|
|
|
* |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
protected $_model; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Fixtures |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $fixtures = array( |
38
|
|
|
'plugin.valid_foreign_key.foreign_main', |
39
|
|
|
'plugin.valid_foreign_key.foreign_one', |
40
|
|
|
'plugin.valid_foreign_key.foreign_two', |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* setUp method |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function setUp() { |
49
|
|
|
parent::setUp(); |
50
|
|
|
$this->_model = ClassRegistry::init('ForeignMain'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* tearDown method |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function tearDown() { |
59
|
|
|
unset($this->_model); |
60
|
|
|
|
61
|
|
|
parent::tearDown(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Tests the default config (no config given) |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
* @coversNothing |
69
|
|
|
*/ |
70
|
|
|
public function testDefaultConfig() { |
71
|
|
|
$this->_loadBehavior(); |
72
|
|
|
|
73
|
|
|
$settings = $this->_model->Behaviors->ValidForeignKey->settings['ForeignMain']; |
74
|
|
|
$expected = array( |
75
|
|
|
'autoValidate' => false, |
76
|
|
|
'errMsg' => 'The key/ID for %s must exist.', |
77
|
|
|
'exclude' => array(), |
78
|
|
|
); |
79
|
|
|
$this->assertSame($expected, $settings); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Tests overwriting the default config. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
* @covers ::setup |
87
|
|
|
*/ |
88
|
|
|
public function testOverwritingConfig() { |
89
|
|
|
$this->_loadBehavior( |
90
|
|
|
array( |
91
|
|
|
'autoValidate' => true, |
92
|
|
|
'errMsg' => 'Error Message', |
93
|
|
|
'exclude' => array('some_field'), |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$settings = $this->_model->Behaviors->ValidForeignKey->settings['ForeignMain']; |
98
|
|
|
$expected = array( |
99
|
|
|
'autoValidate' => true, |
100
|
|
|
'errMsg' => 'Error Message', |
101
|
|
|
'exclude' => array('some_field'), |
102
|
|
|
); |
103
|
|
|
$this->assertSame($expected, $settings); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Tests overwriting the default config using the model name. |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
* @covers ::setup |
111
|
|
|
*/ |
112
|
|
|
public function testOverwritingConfigWithModelName() { |
113
|
|
|
$this->_loadBehavior( |
114
|
|
|
array( |
115
|
|
|
'ForeignMain' => array( |
116
|
|
|
'autoValidate' => true, |
117
|
|
|
'errMsg' => 'Another Error Message', |
118
|
|
|
'exclude' => array('some_other_field'), |
119
|
|
|
) |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$settings = $this->_model->Behaviors->ValidForeignKey->settings['ForeignMain']; |
124
|
|
|
$expected = array( |
125
|
|
|
'autoValidate' => false, |
126
|
|
|
'errMsg' => 'The key/ID for %s must exist.', |
127
|
|
|
'exclude' => array(), |
128
|
|
|
'ForeignMain' => array( |
129
|
|
|
'autoValidate' => true, |
130
|
|
|
'errMsg' => 'Another Error Message', |
131
|
|
|
'exclude' => array('some_other_field'), |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
$this->assertSame($expected, $settings); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Tests that beforeValidate calls validateAllForeignKeys when autoValidate = true. |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
* @covers ::beforeValidate |
142
|
|
|
*/ |
143
|
|
|
public function testAutoValidateCallsMethod() { |
144
|
|
|
$this->_loadBehavior( |
145
|
|
|
array( |
146
|
|
|
'autoValidate' => true, |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
// Mock validateAllForeignKeys method |
151
|
|
|
// Expect it to be called once with the model as parameter. |
152
|
|
|
|
153
|
|
|
$this->markTestIncomplete( |
154
|
|
|
'This test has not been fully implemented yet.' |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Loads the behavior with the given config |
160
|
|
|
* |
161
|
|
|
* Specifically handles the case of setting no config at all. |
162
|
|
|
* |
163
|
|
|
* @param null|array $config Optional Behavior config to set. |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
protected function _loadBehavior($config = null) { |
167
|
|
|
if ($config === null) { |
168
|
|
|
$this->_model->Behaviors->load('ValidForeignKey.ValidForeignKey'); |
169
|
|
|
} else { |
170
|
|
|
$this->_model->Behaviors->load('ValidForeignKey.ValidForeignKey', $config); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |