OSDN Git Service

Collectionableプラグイン導入
[trpgtools-onweb/cake-frame.git] / app / plugins / collectionable / tests / cases / models / behaviors / multi_validation.test.php
1 <?php\r
2 \r
3 class MultiValidaitonMockModel extends CakeTestModel {\r
4 \r
5         var $useTable = false;\r
6         var $validate = array(\r
7                 'email' => array(\r
8                         'email' => array(\r
9                                 'rule' => array('email'),\r
10                         ),\r
11                 ),\r
12                 'title' => array(\r
13                         'notempty' => array(\r
14                                 'rule' => array('notempty'),\r
15                         ),\r
16                 ),\r
17         );\r
18 \r
19         var $validateBestAnswer = array(\r
20                 'best_answer_id' => array(\r
21                         'notempty' => array(\r
22                                 'rule' => array('notempty'),\r
23                         ),\r
24                 ),\r
25         );\r
26 \r
27         var $validateEdit = array(\r
28                 'email' => array(\r
29                         'email' => array(\r
30                                 'rule' => array('email', true),\r
31                         ),\r
32                 ),\r
33         );\r
34 \r
35 }\r
36 \r
37 class MultiValidationBehaviorTestCase extends CakeTestCase {\r
38 \r
39         var $data = array(\r
40                 'MultiValidaitonMockModel' => array(\r
41                         'nickname' => '0123456789012',\r
42                 ),\r
43         );\r
44 \r
45         function startTest() {\r
46                 $this->_attach();\r
47         }\r
48 \r
49         function endTest() {\r
50                 $this->_clear();\r
51         }\r
52 \r
53         function _attach($settings = array()) {\r
54 \r
55                 $this->Model = ClassRegistry::init('MultiValidaitonMockModel');\r
56                 $this->Model->Behaviors->detach('Collectionable.MultiValidation');\r
57                 $this->Model->Behaviors->attach('Collectionable.MultiValidation', $settings);\r
58 \r
59         }\r
60 \r
61         function _clear() {\r
62 \r
63                 unset($this->Model);\r
64                 ClassRegistry::flush();\r
65 \r
66         }\r
67 \r
68         function _reattach($settings = array()) {\r
69 \r
70                 $this->_clear();\r
71                 $this->_attach($settings);\r
72 \r
73         }\r
74 \r
75         function testUseValidationSet() {\r
76 \r
77                 $this->Model->useValidationSet('BestAnswer');\r
78                 $result = $this->Model->validate;\r
79                 $expected = array(\r
80                         'email' => array(\r
81                                 'email' => array(\r
82                                         'rule' => array('email'),\r
83                                 ),\r
84                         ),\r
85                         'title' => array(\r
86                                 'notempty' => array(\r
87                                         'rule' => array('notempty'),\r
88                                 ),\r
89                         ),\r
90                         'best_answer_id' => array(\r
91                                 'notempty' => array(\r
92                                         'rule' => array('notempty'),\r
93                                 ),\r
94                         ),\r
95                 );\r
96                 $this->assertIdentical($result, $expected);\r
97 \r
98                 $this->Model->restoreValidate();\r
99                 $this->Model->useValidationSet('bestAnswer');\r
100                 $result = $this->Model->validate;\r
101                 $this->assertIdentical($result, $expected);\r
102 \r
103                 $this->Model->restoreValidate();\r
104                 $this->Model->useValidationSet('BestAnswer', false);\r
105                 $result = $this->Model->validate;\r
106                 $expected = array(\r
107                         'best_answer_id' => array(\r
108                                 'notempty' => array(\r
109                                         'rule' => array('notempty'),\r
110                                 ),\r
111                         ),\r
112                 );\r
113                 $this->assertIdentical($result, $expected);\r
114 \r
115                 $this->Model->restoreValidate();\r
116                 $this->Model->useValidationSet(array('bestAnswer', 'edit'));\r
117                 $result = $this->Model->validate;\r
118                 $expected = array(\r
119                         'email' => array(\r
120                                 'email' => array(\r
121                                         'rule' => array('email', true),\r
122                                 ),\r
123                         ),\r
124                         'title' => array(\r
125                                 'notempty' => array(\r
126                                         'rule' => array('notempty'),\r
127                                 ),\r
128                         ),\r
129                         'best_answer_id' => array(\r
130                                 'notempty' => array(\r
131                                         'rule' => array('notempty'),\r
132                                 ),\r
133                         ),\r
134                 );\r
135                 $this->assertIdentical($result, $expected);\r
136 \r
137                 $this->expectError();\r
138                 $this->assertFalse($this->Model->useValidationSet('invalidPropertyName'));\r
139 \r
140         }\r
141 \r
142         function testAfterSave() {\r
143 \r
144                 $this->Model->useValidationSet('bestAnswer', false);\r
145                 $this->Model->Behaviors->MultiValidation->afterSave($this->Model);\r
146                 $expected = array(\r
147                         'email' => array(\r
148                                 'email' => array(\r
149                                         'rule' => array('email'),\r
150                                 ),\r
151                         ),\r
152                         'title' => array(\r
153                                 'notempty' => array(\r
154                                         'rule' => array('notempty'),\r
155                                 ),\r
156                         ),\r
157                 );\r
158                 $this->assertIdentical($this->Model->validate, $expected);\r
159 \r
160                 $this->_reattach(array('restore' => false));\r
161 \r
162                 $this->Model->useValidationSet('bestAnswer', false);\r
163                 $this->Model->Behaviors->MultiValidation->afterSave($this->Model);\r
164                 $expected = array(\r
165                         'best_answer_id' => array(\r
166                                 'notempty' => array(\r
167                                         'rule' => array('notempty'),\r
168                                 ),\r
169                         ),\r
170                 );\r
171                 $this->assertIdentical($this->Model->validate, $expected);\r
172 \r
173         }\r
174 \r
175 }\r