Révision ca0757b9
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/file_entity/file_entity.test | ||
---|---|---|
17 | 17 |
parent::setUp($modules); |
18 | 18 |
} |
19 | 19 |
|
20 |
protected function setUpFiles() { |
|
20 |
protected function setUpFiles($defaults = array()) { |
|
21 |
// Populate defaults array. |
|
22 |
$defaults += array( |
|
23 |
'uid' => 1, |
|
24 |
'status' => FILE_STATUS_PERMANENT, |
|
25 |
); |
|
26 |
|
|
21 | 27 |
$types = array('text', 'image'); |
22 | 28 |
foreach ($types as $type) { |
23 | 29 |
foreach ($this->drupalGetTestFiles($type) as $file) { |
30 |
foreach ($defaults as $key => $value) { |
|
31 |
$file->$key = $value; |
|
32 |
} |
|
24 | 33 |
$this->files[$type][] = file_save($file); |
25 | 34 |
} |
26 | 35 |
} |
... | ... | |
261 | 270 |
|
262 | 271 |
function setUp() { |
263 | 272 |
parent::setUp(); |
264 |
parent::setUpFiles();
|
|
273 |
$this->setUpFiles();
|
|
265 | 274 |
} |
266 | 275 |
|
267 | 276 |
/** |
... | ... | |
665 | 674 |
|
666 | 675 |
function setUp() { |
667 | 676 |
parent::setUp(); |
668 |
parent::setUpFiles();
|
|
677 |
$this->setUpFiles();
|
|
669 | 678 |
} |
670 | 679 |
|
671 | 680 |
/** |
... | ... | |
705 | 714 |
$this->assertEqual($file->filename, $original->filename, 'Updated file name did not change.'); |
706 | 715 |
$this->assertNotEqual($file->filesize, $original->filesize, 'Updated file size changed from previous file.'); |
707 | 716 |
$this->assertEqual($file->filesize, $replacement->filesize, 'Updated file size matches uploaded file.'); |
708 |
$this->assertEqual(file_get_contents($replacement->uri), file_get_contents($file->uri), 'Updated file contents matches uploaded file.'); |
|
717 |
$this->assertEqual(file_get_contents($file->uri), file_get_contents($replacement->uri), 'Updated file contents matches uploaded file.'); |
|
718 |
$this->assertFalse(entity_load('file', FALSE, array('status' => 0)), 'Temporary file used for replacement was deleted.'); |
|
709 | 719 |
|
710 | 720 |
// Get an image file. |
711 | 721 |
$image = reset($this->files['image']); |
... | ... | |
746 | 756 |
|
747 | 757 |
function setUp() { |
748 | 758 |
parent::setUp(); |
749 |
parent::setUpFiles();
|
|
759 |
$this->setUpFiles();
|
|
750 | 760 |
} |
751 | 761 |
|
752 | 762 |
function testFileEntityTokens() { |
... | ... | |
800 | 810 |
|
801 | 811 |
function setUp() { |
802 | 812 |
parent::setUp(); |
803 |
parent::setUpFiles();
|
|
813 |
$this->setUpFiles();
|
|
804 | 814 |
} |
805 | 815 |
|
806 | 816 |
/** |
... | ... | |
1069 | 1079 |
|
1070 | 1080 |
function setUp() { |
1071 | 1081 |
parent::setUp(); |
1072 |
parent::setUpFiles(); |
|
1082 |
$this->setUpFiles(array('uid' => 0)); |
|
1083 |
|
|
1084 |
// Unset the fact that file_entity_install() adds the 'view files' |
|
1085 |
// permission to all user roles. This messes with being able to fully unit |
|
1086 |
// test the file_entity_access() function. |
|
1087 |
$roles = user_roles(); |
|
1088 |
foreach ($roles as $rid => $role) { |
|
1089 |
user_role_revoke_permissions($rid, array('view files')); |
|
1090 |
} |
|
1073 | 1091 |
} |
1074 | 1092 |
|
1075 | 1093 |
/** |
1076 | 1094 |
* Asserts file_entity_access correctly grants or denies access. |
1077 | 1095 |
*/ |
1078 | 1096 |
function assertFileEntityAccess($ops, $file, $account) { |
1097 |
drupal_static_reset('file_entity_access'); |
|
1079 | 1098 |
foreach ($ops as $op => $result) { |
1080 | 1099 |
$msg = t("file_entity_access returns @result with operation '@op'.", array('@result' => $result ? 'true' : 'false', '@op' => $op)); |
1081 | 1100 |
$this->assertEqual($result, file_entity_access($op, $file, $account), $msg); |
... | ... | |
1107 | 1126 |
$web_user = $this->drupalCreateUser(array('create files', 'view own files')); |
1108 | 1127 |
$this->assertFileEntityAccess(array('view' => FALSE), $file, $web_user); |
1109 | 1128 |
$file->uid = $web_user->uid; |
1110 |
$file->status = FILE_STATUS_PERMANENT; |
|
1111 |
file_save($file); |
|
1112 | 1129 |
$this->assertFileEntityAccess(array('view' => TRUE), $file, $web_user); |
1113 | 1130 |
|
1114 | 1131 |
// User can download own files but no other files. |
1115 | 1132 |
$web_user = $this->drupalCreateUser(array('create files', 'download own image files')); |
1116 | 1133 |
$this->assertFileEntityAccess(array('download' => FALSE), $file, $web_user); |
1117 | 1134 |
$file->uid = $web_user->uid; |
1118 |
$file->status = FILE_STATUS_PERMANENT; |
|
1119 |
file_save($file); |
|
1120 | 1135 |
$this->assertFileEntityAccess(array('download' => TRUE), $file, $web_user); |
1121 | 1136 |
|
1122 | 1137 |
// User can update own files but no other files. |
1123 | 1138 |
$web_user = $this->drupalCreateUser(array('create files', 'view own files', 'edit own image files')); |
1124 | 1139 |
$this->assertFileEntityAccess(array('update' => FALSE), $file, $web_user); |
1125 | 1140 |
$file->uid = $web_user->uid; |
1126 |
$file->status = FILE_STATUS_PERMANENT; |
|
1127 |
file_save($file); |
|
1128 | 1141 |
$this->assertFileEntityAccess(array('update' => TRUE), $file, $web_user); |
1129 | 1142 |
|
1130 | 1143 |
// User can delete own files but no other files. |
1131 | 1144 |
$web_user = $this->drupalCreateUser(array('create files', 'view own files', 'edit own image files', 'delete own image files')); |
1132 | 1145 |
$this->assertFileEntityAccess(array('delete' => FALSE), $file, $web_user); |
1133 | 1146 |
$file->uid = $web_user->uid; |
1134 |
$file->status = FILE_STATUS_PERMANENT; |
|
1135 |
file_save($file); |
|
1136 | 1147 |
$this->assertFileEntityAccess(array('delete' => TRUE), $file, $web_user); |
1137 | 1148 |
|
1138 | 1149 |
// User can view any file. |
... | ... | |
1172 | 1183 |
$this->assertResponse(200, 'Users with access can access the file add page'); |
1173 | 1184 |
|
1174 | 1185 |
$file = reset($this->files['text']); |
1175 |
$file->status = FILE_STATUS_PERMANENT; |
|
1176 |
file_save($file); |
|
1177 | 1186 |
|
1178 | 1187 |
// This fails.. No clue why but, tested manually and works as should. |
1179 | 1188 |
//$web_user = $this->drupalCreateUser(array('view own files')); |
... | ... | |
1188 | 1197 |
$url = "file/{$file->fid}/download"; |
1189 | 1198 |
$web_user = $this->drupalCreateUser(array()); |
1190 | 1199 |
$this->drupalLogin($web_user); |
1191 |
$this->drupalGet($url, array('query' => array('token' => $this->drupalGetToken($url))));
|
|
1200 |
$this->drupalGet($url, array('query' => array('token' => file_entity_get_download_token($file))));
|
|
1192 | 1201 |
$this->assertResponse(403, 'Users without access can not download the file'); |
1193 | 1202 |
$web_user = $this->drupalCreateUser(array('download any document files')); |
1194 | 1203 |
$this->drupalLogin($web_user); |
1195 |
$this->drupalGet($url, array('query' => array('token' => $this->drupalGetToken($url))));
|
|
1204 |
$this->drupalGet($url, array('query' => array('token' => file_entity_get_download_token($file))));
|
|
1196 | 1205 |
$this->assertResponse(200, 'Users with access can download the file'); |
1197 | 1206 |
$this->drupalGet($url, array('query' => array('token' => 'invalid-token'))); |
1198 | 1207 |
$this->assertResponse(403, 'Cannot download file with in invalid token.'); |
1199 | 1208 |
$this->drupalGet($url); |
1200 | 1209 |
$this->assertResponse(403, 'Cannot download file without a token.'); |
1210 |
variable_set('file_entity_allow_insecure_download', TRUE); |
|
1211 |
$this->drupalGet($url); |
|
1212 |
$this->assertResponse(200, 'Users with access can download the file without a token when file_entity_allow_insecure_download is set.'); |
|
1201 | 1213 |
|
1202 | 1214 |
$web_user = $this->drupalCreateUser(array()); |
1203 | 1215 |
$this->drupalLogin($web_user); |
... | ... | |
1233 | 1245 |
// Create private, permanent files owned by this user only he's an owner. |
1234 | 1246 |
if (!empty($case['owner'])) { |
1235 | 1247 |
$file = next($this->files['text']); |
1236 |
$file->status = FILE_STATUS_PERMANENT; |
|
1237 | 1248 |
$file->uid = $account->uid; |
1238 | 1249 |
file_save($file); |
1239 | 1250 |
$file = file_move($file, 'private://'); |
... | ... | |
1256 | 1267 |
} |
1257 | 1268 |
} |
1258 | 1269 |
} |
1270 |
|
|
1271 |
class FileEntityAttributeOverrideTestCase extends FileEntityTestHelper { |
|
1272 |
|
|
1273 |
public static function getInfo() { |
|
1274 |
return array( |
|
1275 |
'name' => 'File entity attribute override', |
|
1276 |
'description' => 'Test overriding file entity attributes.', |
|
1277 |
'group' => 'File entity', |
|
1278 |
); |
|
1279 |
} |
|
1280 |
|
|
1281 |
function setUp() { |
|
1282 |
parent::setUp(); |
|
1283 |
$this->setUpFiles(); |
|
1284 |
} |
|
1285 |
|
|
1286 |
/** |
|
1287 |
* Test to see if file attributes can be overridden. |
|
1288 |
*/ |
|
1289 |
function testFileEntityFileAttributeOverrides() { |
|
1290 |
$overrides = array( |
|
1291 |
'width' => 40, |
|
1292 |
'height' => 20, |
|
1293 |
'alt' => $this->randomName(), |
|
1294 |
'title' => $this->randomName(), |
|
1295 |
|
|
1296 |
); |
|
1297 |
|
|
1298 |
// Retrieve an image file entity for testing. |
|
1299 |
$file = reset($this->files['image']); |
|
1300 |
|
|
1301 |
// Override a variety of attributes. |
|
1302 |
foreach ($overrides as $override => $value) { |
|
1303 |
$file->override['attributes'][$override] = $value; |
|
1304 |
} |
|
1305 |
|
|
1306 |
// Test that the attributes have been overridden. |
|
1307 |
$build = file_view_file($file, 'full'); |
|
1308 |
|
|
1309 |
foreach ($overrides as $attribute => $expected_value) { |
|
1310 |
$this->assertEqual($build['#item'][$attribute], $expected_value, format_string('The %attribute was overridden correctly.', array('%attribute' => $attribute))); |
|
1311 |
} |
|
1312 |
} |
|
1313 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules