Skip to content

Commit 52c9cd7

Browse files
committed
Page::deletePage()のテスト
1 parent cce0cd6 commit 52c9cd7

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
<?php
2+
/**
3+
* Page::deletePage()のテスト
4+
*
5+
* @author Noriko Arai <arai@nii.ac.jp>
6+
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
7+
* @link http://www.netcommons.org NetCommons Project
8+
* @license http://www.netcommons.org/license.txt NetCommons License
9+
* @copyright Copyright 2014, NetCommons Project
10+
*/
11+
12+
App::uses('NetCommonsDeleteTest', 'NetCommons.TestSuite');
13+
App::uses('PageFixture', 'Pages.Test/Fixture');
14+
15+
/**
16+
* Page::deletePage()のテスト
17+
*
18+
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
19+
* @package NetCommons\Pages\Test\Case\Model\Page
20+
*/
21+
class PageDeletePageTest extends NetCommonsDeleteTest {
22+
23+
/**
24+
* Fixtures
25+
*
26+
* @var array
27+
*/
28+
public $fixtures = array(
29+
'plugin.pages.box4pages',
30+
'plugin.pages.boxes_page4pages',
31+
'plugin.pages.container4pages',
32+
'plugin.pages.containers_page4pages',
33+
'plugin.pages.frame4pages',
34+
'plugin.pages.languages_page4pages',
35+
'plugin.pages.page4pages',
36+
'plugin.pages.plugin4pages',
37+
'plugin.pages.plugins_room4pages',
38+
);
39+
40+
/**
41+
* Plugin name
42+
*
43+
* @var string
44+
*/
45+
public $plugin = 'pages';
46+
47+
/**
48+
* Model name
49+
*
50+
* @var string
51+
*/
52+
protected $_modelName = 'Page';
53+
54+
/**
55+
* Method name
56+
*
57+
* @var string
58+
*/
59+
protected $_methodName = 'deletePage';
60+
61+
/**
62+
* setUp method
63+
*
64+
* @return void
65+
*/
66+
public function setUp() {
67+
parent::setUp();
68+
69+
//テストクエリ
70+
$this->___query = array(
71+
'recursive' => -1,
72+
'fields' => array('id', 'parent_id', 'lft', 'rght'),
73+
'conditions' => array('id' => array('1', '4', '7', '8')),
74+
'order' => array('lft' => 'asc'),
75+
);
76+
//事前チェック
77+
$model = $this->_modelName;
78+
$result = $this->$model->find('all', $this->___query);
79+
$expected = array(
80+
0 => array('Page' => array(
81+
'id' => '1', 'parent_id' => null, 'lft' => '1', 'rght' => '8'
82+
)),
83+
1 => array('Page' => array(
84+
'id' => '4', 'parent_id' => '1', 'lft' => '2', 'rght' => '5'
85+
)),
86+
2 => array('Page' => array(
87+
'id' => '7', 'parent_id' => '4', 'lft' => '3', 'rght' => '4'
88+
)),
89+
3 => array('Page' => array(
90+
'id' => '8', 'parent_id' => '1', 'lft' => '6', 'rght' => '7'
91+
)),
92+
);
93+
$this->assertEquals($expected, $result);
94+
95+
//事前チェック用
96+
$this->LanguagesPage = ClassRegistry::init('Pages.LanguagesPage');
97+
}
98+
99+
/**
100+
* Delete用DataProvider
101+
*
102+
* ### 戻り値
103+
* - data: 削除データ
104+
* - associationModels: 削除確認の関連モデル array(model => conditions)
105+
*
106+
* @return array テストデータ
107+
*/
108+
public function dataProviderDelete() {
109+
$data['Page'] = array('id' => '7');
110+
$association = array();
111+
112+
$results = array();
113+
$results[0] = array($data, $association);
114+
115+
return $results;
116+
}
117+
118+
/**
119+
* Deleteのテスト
120+
*
121+
* @param array|string $data 削除データ
122+
* @param array $associationModels 削除確認の関連モデル array(model => conditions)
123+
* @dataProvider dataProviderDelete
124+
* @return void
125+
*/
126+
public function testDelete($data, $associationModels = null) {
127+
$model = $this->_modelName;
128+
$method = $this->_methodName;
129+
130+
$this->$model = $this->getMockForModel('Pages.Page', array('begin', 'commit', 'deleteContainers', 'deleteBoxes'));
131+
$this->_mockForReturnTrue($model, 'Pages.Page', array('deleteContainers', 'deleteBoxes'));
132+
$this->_mockForReturnTrue($model, 'Pages.Page', array('begin', 'commit'));
133+
134+
//事前チェック
135+
// * Pageの事前チェックはsetUpで行っている
136+
$count = $this->LanguagesPage->find('count', array(
137+
'recursive' => -1,
138+
'conditions' => array('page_id' => Hash::get($data, 'Page.id'))
139+
));
140+
$this->assertEquals(2, $count);
141+
142+
//テスト実施
143+
$result = $this->$model->$method($data);
144+
$this->assertTrue($result);
145+
146+
//チェック
147+
$this->__assertDelete($data);
148+
}
149+
150+
/**
151+
* Deleteのテスト(atomic=falseの場合)
152+
*
153+
* @param array|string $data 削除データ
154+
* @param array $associationModels 削除確認の関連モデル array(model => conditions)
155+
* @dataProvider dataProviderDelete
156+
* @return void
157+
*/
158+
public function testDeleteOptionAtomic($data, $associationModels = null) {
159+
$model = $this->_modelName;
160+
$method = $this->_methodName;
161+
162+
$this->$model = $this->getMockForModel('Pages.Page', array('begin', 'commit', 'deleteContainers', 'deleteBoxes'));
163+
$this->_mockForReturnTrue($model, 'Pages.Page', array('deleteContainers', 'deleteBoxes'));
164+
$this->_mockForReturnTrue($model, 'Pages.Page', array('begin', 'commit'), 0);
165+
166+
//事前チェック
167+
// * Pageの事前チェックはsetUpで行っている
168+
$count = $this->LanguagesPage->find('count', array(
169+
'recursive' => -1,
170+
'conditions' => array('page_id' => Hash::get($data, 'Page.id'))
171+
));
172+
$this->assertEquals(2, $count);
173+
174+
//テスト実施
175+
$result = $this->$model->$method($data, array('atomic' => false));
176+
$this->assertTrue($result);
177+
178+
//チェック
179+
$this->__assertDelete($data);
180+
}
181+
182+
/**
183+
* Deleteのチェック
184+
*
185+
* @param array|string $data 削除データ
186+
* @return void
187+
*/
188+
private function __assertDelete($data) {
189+
$model = $this->_modelName;
190+
191+
//Pageのチェック
192+
$result = $this->$model->find('all', $this->___query);
193+
$expected = array(
194+
0 => array('Page' => array(
195+
'id' => '1', 'parent_id' => null, 'lft' => '1', 'rght' => '6'
196+
)),
197+
1 => array('Page' => array(
198+
'id' => '4', 'parent_id' => '1', 'lft' => '2', 'rght' => '3'
199+
)),
200+
2 => array('Page' => array(
201+
'id' => '8', 'parent_id' => '1', 'lft' => '4', 'rght' => '5'
202+
)),
203+
);
204+
$this->assertEquals($expected, $result);
205+
206+
//LanguagesPageのチェック
207+
$count = $this->LanguagesPage->find('count', array(
208+
'recursive' => -1,
209+
'conditions' => array('page_id' => Hash::get($data, 'Page.id'))
210+
));
211+
$this->assertEquals(0, $count);
212+
}
213+
214+
/**
215+
* ExceptionError用DataProvider
216+
*
217+
* ### 戻り値
218+
* - data 登録データ
219+
* - mockModel Mockのモデル
220+
* - mockMethod Mockのメソッド
221+
*
222+
* @return array テストデータ
223+
*/
224+
public function dataProviderDeleteOnExceptionError() {
225+
$data = $this->dataProviderDelete()[0][0];
226+
227+
return array(
228+
array($data, 'Pages.Page', 'delete'),
229+
array($data, 'Pages.LanguagesPage', 'deleteAll'),
230+
);
231+
}
232+
233+
/**
234+
* DeleteのExceptionErrorテスト
235+
*
236+
* @param array $data 登録データ
237+
* @param string $mockModel Mockのモデル
238+
* @param string $mockMethod Mockのメソッド
239+
* @dataProvider dataProviderDeleteOnExceptionError
240+
* @return void
241+
*/
242+
public function testDeleteOnExceptionError($data, $mockModel, $mockMethod) {
243+
$model = $this->_modelName;
244+
$method = $this->_methodName;
245+
246+
$this->$model = $this->getMockForModel('Pages.Page', array('begin', 'rollback', $mockMethod));
247+
$this->_mockForReturnFalse($model, $mockModel, $mockMethod);
248+
$this->_mockForReturnTrue($model, 'Pages.Page', array('begin', 'rollback'));
249+
250+
$this->setExpectedException('InternalErrorException');
251+
$this->$model->$method($data);
252+
}
253+
254+
/**
255+
* DeleteのExceptionErrorテスト(atomic=falseの場合)
256+
*
257+
* @param array $data 登録データ
258+
* @param string $mockModel Mockのモデル
259+
* @param string $mockMethod Mockのメソッド
260+
* @dataProvider dataProviderDeleteOnExceptionError
261+
* @return void
262+
*/
263+
public function testDeleteOptionAtomicOnExceptionError($data, $mockModel, $mockMethod) {
264+
$model = $this->_modelName;
265+
$method = $this->_methodName;
266+
267+
$this->$model = $this->getMockForModel('Pages.Page', array('begin', 'rollback', $mockMethod));
268+
$this->_mockForReturnTrue($model, 'Pages.Page', array('begin', 'rollback'), 0);
269+
$this->_mockForReturnFalse($model, $mockModel, $mockMethod);
270+
271+
$this->setExpectedException('InternalErrorException');
272+
$this->$model->$method($data, array('atomic' => false));
273+
}
274+
275+
}

0 commit comments

Comments
 (0)