-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCreateTest.php
More file actions
189 lines (162 loc) · 4.83 KB
/
CreateTest.php
File metadata and controls
189 lines (162 loc) · 4.83 KB
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
189
<?php
/**
* NetCommonsApp Test Case
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('Current', 'NetCommons.Utility');
App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite');
App::uses('NetCommonsControllerTestCase', 'NetCommons.TestSuite');
/**
* Summary for NetCommonsApp Test Case
*/
class NetCommonsAppModelCreateTest extends NetCommonsCakeTestCase {
/**
* @var array fixture
*/
public $fixtures = array(
'plugin.site_manager.site_setting',
'plugin.net_commons.create_profile',
'plugin.net_commons.create_default_value',
);
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
NetCommonsControllerTestCase::loadTestPlugin($this, 'NetCommons', 'TestNetCommons');
Current::$current['Language']['id'] = '5';
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
Current::$current = array();
parent::tearDown();
}
/**
* createでnot nullカラムのデフォルトがnullにならないこと
*
* @see https://github.com/NetCommons3/NetCommons3/issues/7
* @return void
*/
public function testNotNullFieldDefaultIsNotNull() {
$SiteSetting = ClassRegistry::init('SiteManager.SiteSetting');
$newData = $SiteSetting->create();
// Tableでnot null なカラムにnullがセットされちゃダメ
$this->assertNotNull($newData['SiteSetting']['key']);
$this->assertNotNull($newData['SiteSetting']['label']);
// CurrentがあればCurrentで上書きされる
App::uses('Current', 'NetCommons.Utility');
Current::$current['Language']['id'] = 5;
$newDataWithCurrent = $SiteSetting->create();
$this->assertEquals(5, $newDataWithCurrent['SiteSetting']['language_id']);
// $data が渡れれば$dataを優先する
$data = array();
$data['SiteSetting']['language_id'] = 10;
$newDataWithCurrent = $SiteSetting->create($data);
$this->assertEquals(10, $newDataWithCurrent['SiteSetting']['language_id']);
// nullを渡すと空が返る
$newDataWithCurrent = $SiteSetting->create(null);
$this->assertEmpty($newDataWithCurrent);
// falseを渡すと空が返る
$newDataWithCurrent = $SiteSetting->create(false);
$this->assertEmpty($newDataWithCurrent);
}
/**
* create()でモデル名付きの配列を渡すとデフォルト値がセットされなかったバグの修正テスト
* ```
* var_dump($this->Announcement->create(array('Announcement' => array(
* 'id' => null,
* ))));
* var_dump($this->Announcement->create(array(
* 'id' => null,
* )));
* 上記の結果が同じになるように修正した
* ```
*
* @return void
*/
public function testCreateWithModelNameData() {
$TestCreateProfile = ClassRegistry::init('TestNetCommons.TestCreateProfile');
$data = [
'name' => 'foo'
];
$newData = $TestCreateProfile->create($data);
$dataWithModelName = [
'TestCreateProfile' => [
'name' => 'foo'
]
];
$newDataWithModelName = $TestCreateProfile->create($dataWithModelName);
$this->assertEquals($newData, $newDataWithModelName);
$this->assertEquals(5, $newData['TestCreateProfile']['language_id']);
}
/**
* create()でデフォルト値のセットテスト
*
* @return void
*/
public function testCreateDefaultValue() {
//事前準備
$Model = ClassRegistry::init('TestNetCommons.TestCreateDefaultValue');
//テストデータ
Current::write('Room.id', '1');
Current::write('Language.id', '2');
Current::write('Block.id', '3');
Current::write('Block.key', 'block_key1');
Current::write('Frame.id', '6');
Current::write('Frame.key', 'frame_key1');
$data = array('name' => 'foo');
//テスト実施
$newData = $Model->create($data);
$expected = array(
'TestCreateDefaultValue' => array(
'room_id' => '1',
'language_id' => '2',
'block_id' => '3',
'block_key' => 'block_key1',
'frame_id' => '6',
'frame_key' => 'frame_key1',
'plugin_key' => 'test_net_commons',
'name' => 'foo',
'created_user' => null,
'created' => null,
'modified_user' => null,
'modified' => null,
)
);
$this->assertEquals($newData, $expected);
}
/**
* useTableがfalseの場合、デフォルト値をセットしないテスト
*
* @return void
*/
public function testCreateWOTable() {
//事前準備
$Model = ClassRegistry::init('TestNetCommons.TestCreateNotTable');
//テストデータ
Current::write('Room.id', '1');
Current::write('Language.id', '2');
Current::write('Block.id', '3');
Current::write('Block.key', 'block_key1');
Current::write('Frame.id', '6');
Current::write('Frame.key', 'frame_key1');
$data = array('name' => 'foo');
//テスト実施
$newData = $Model->create($data);
$expected = array(
'TestCreateNotTable' => array(
'name' => 'foo',
)
);
$this->assertEquals($newData, $expected);
}
}