-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDeleteLinkTest.php
More file actions
143 lines (125 loc) · 3.81 KB
/
DeleteLinkTest.php
File metadata and controls
143 lines (125 loc) · 3.81 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
<?php
/**
* UploadFile::deleteLink()のテスト
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('NetCommonsModelTestCase', 'NetCommons.TestSuite');
App::uses('UploadFileFixture', 'Files.Test/Fixture');
App::uses('TemporaryFolder', 'Files.Utility');
App::uses('TemporaryFile', 'Files.Utility');
/**
* UploadFile::deleteLink()のテスト
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @package NetCommons\Files\Test\Case\Model\UploadFile
*/
class UploadFileDeleteLinkTest extends NetCommonsModelTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.files.upload_file',
'plugin.files.upload_files_content',
);
/**
* Plugin name
*
* @var string
*/
public $plugin = 'files';
/**
* Model name
*
* @var string
*/
protected $_modelName = 'UploadFile';
/**
* Method name
*
* @var string
*/
protected $_methodName = 'deleteLink';
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
//$this->UploadFile = ClassRegistry::init('Files.UploadFile');
$this->UploadFilesContent = ClassRegistry::init('Files.UploadFilesContent');
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
unset($this->UploadFileContent);
parent::tearDown();
}
/**
* testDeleteLink method
*
* @return void
*/
public function testDeleteLink() {
$pluginKey = 'site_manager';
$contentId = 2;
$fieldName = 'photo';
// Uploadビヘイビアが実ファイルを削除しにくるので事前に削除されるファイルを用意しておく
$tmpFolder = new TemporaryFolder();
$this->UploadFile->uploadBasePath = $tmpFolder->path . '/';
$tmpFile = new TemporaryFile($tmpFolder->path . '/files/upload_file/real_file_name/1/1/');
rename($tmpFile->path, dirname($tmpFile->path) . '/foobarhash.jpg');
$tmpFile->name = 'foobarhash.jpg';
$tmpFile->path = dirname($tmpFile->path) . '/foobarhash.jpg';
$this->UploadFile->deleteLink($pluginKey, $contentId, $fieldName);
// fixtureで挿入された関連レコードが削除されてること
$count = $this->UploadFilesContent->find('count', ['conditions' => ['UploadFilesContent.id' => 1]]);
$this->assertEquals(0, $count);
// fixtureで挿入されたUploadFileレコードも削除されること(他に関連がないので)
$count = $this->UploadFile->find('count', ['conditions' => ['UploadFile.id' => 1]]);
$this->assertEquals(0, $count);
}
/**
* deleteLink delete()失敗で例外発生
*
* @return void
*/
public function testDeleteLinkFailed() {
$pluginKey = 'site_manager';
$contentId = 2;
$fieldName = 'photo';
// Uploadビヘイビアを無効にしておく
$this->UploadFile->Behaviors->unload('Upload');
$this->_mockForReturnFalse('UploadFile', 'Files.UploadFile', 'delete');
$this->setExpectedException('InternalErrorException');
$this->UploadFile->deleteLink($pluginKey, $contentId, $fieldName);
}
/**
* deleteLink コンテンツとのリンクがないケース
*
* @return void
*/
public function testDeleteLinkByNoLink() {
$pluginKey = 'site_manager';
$contentId = 3;
$fieldName = 'photo';
// Uploadビヘイビアを無効にしておく
$this->UploadFile->Behaviors->unload('Upload');
$this->UploadFile->deleteLink($pluginKey, $contentId, $fieldName);
// 削除されるリンクがないので、レコード数に変化無し
$count = $this->UploadFilesContent->find('count', ['conditions' => ['UploadFilesContent.id' => 1]]);
$this->assertEquals(1, $count);
// 削除されるリンクがないので、レコード数に変化無し
$count = $this->UploadFile->find('count', ['conditions' => ['UploadFile.id' => 1]]);
$this->assertEquals(1, $count);
}
}