[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorFileTestCase extends PhabricatorTestCase { 4 5 public function getPhabricatorTestCaseConfiguration() { 6 return array( 7 self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true, 8 ); 9 } 10 11 public function testFileVisibility() { 12 $engine = new PhabricatorTestStorageEngine(); 13 $data = Filesystem::readRandomCharacters(64); 14 15 $author = $this->generateNewTestUser(); 16 $viewer = $this->generateNewTestUser(); 17 $users = array($author, $viewer); 18 19 $params = array( 20 'name' => 'test.dat', 21 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, 22 'authorPHID' => $author->getPHID(), 23 'storageEngines' => array( 24 $engine, 25 ), 26 ); 27 28 $file = PhabricatorFile::newFromFileData($data, $params); 29 $filter = new PhabricatorPolicyFilter(); 30 31 // Test bare file policies. 32 $this->assertEqual( 33 array( 34 true, 35 false, 36 ), 37 $this->canViewFile($users, $file), 38 pht('File Visibility')); 39 40 // Create an object and test object policies. 41 42 $object = ManiphestTask::initializeNewTask($author); 43 $object->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy()); 44 $object->save(); 45 46 $this->assertTrue( 47 $filter->hasCapability( 48 $author, 49 $object, 50 PhabricatorPolicyCapability::CAN_VIEW), 51 pht('Object Visible to Author')); 52 53 $this->assertTrue( 54 $filter->hasCapability( 55 $viewer, 56 $object, 57 PhabricatorPolicyCapability::CAN_VIEW), 58 pht('Object Visible to Others')); 59 60 // Attach the file to the object and test that the association opens a 61 // policy exception for the non-author viewer. 62 63 $file->attachToObject($object->getPHID()); 64 65 // Test the attached file's visibility. 66 $this->assertEqual( 67 array( 68 true, 69 true, 70 ), 71 $this->canViewFile($users, $file), 72 pht('Attached File Visibility')); 73 74 // Create a "thumbnail" of the original file. 75 $params = array( 76 'name' => 'test.thumb.dat', 77 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, 78 'storageEngines' => array( 79 $engine, 80 ), 81 ); 82 83 $xform = PhabricatorFile::newFromFileData($data, $params); 84 85 id(new PhabricatorTransformedFile()) 86 ->setOriginalPHID($file->getPHID()) 87 ->setTransform('test-thumb') 88 ->setTransformedPHID($xform->getPHID()) 89 ->save(); 90 91 // Test the thumbnail's visibility. 92 $this->assertEqual( 93 array( 94 true, 95 true, 96 ), 97 $this->canViewFile($users, $xform), 98 pht('Attached Thumbnail Visibility')); 99 100 // Detach the object and make sure it affects the thumbnail. 101 $file->detachFromObject($object->getPHID()); 102 103 // Test the detached thumbnail's visibility. 104 $this->assertEqual( 105 array( 106 true, 107 false, 108 ), 109 $this->canViewFile($users, $xform), 110 pht('Detached Thumbnail Visibility')); 111 } 112 113 private function canViewFile(array $users, PhabricatorFile $file) { 114 $results = array(); 115 foreach ($users as $user) { 116 $results[] = (bool)id(new PhabricatorFileQuery()) 117 ->setViewer($user) 118 ->withPHIDs(array($file->getPHID())) 119 ->execute(); 120 } 121 return $results; 122 } 123 124 public function testFileStorageReadWrite() { 125 $engine = new PhabricatorTestStorageEngine(); 126 127 $data = Filesystem::readRandomCharacters(64); 128 129 $params = array( 130 'name' => 'test.dat', 131 'storageEngines' => array( 132 $engine, 133 ), 134 ); 135 136 $file = PhabricatorFile::newFromFileData($data, $params); 137 138 // Test that the storage engine worked, and was the target of the write. We 139 // don't actually care what the data is (future changes may compress or 140 // encrypt it), just that it exists in the test storage engine. 141 $engine->readFile($file->getStorageHandle()); 142 143 // Now test that we get the same data back out. 144 $this->assertEqual($data, $file->loadFileData()); 145 } 146 147 public function testFileStorageUploadDifferentFiles() { 148 $engine = new PhabricatorTestStorageEngine(); 149 150 $data = Filesystem::readRandomCharacters(64); 151 $other_data = Filesystem::readRandomCharacters(64); 152 153 $params = array( 154 'name' => 'test.dat', 155 'storageEngines' => array( 156 $engine, 157 ), 158 ); 159 160 $first_file = PhabricatorFile::newFromFileData($data, $params); 161 162 $second_file = PhabricatorFile::newFromFileData($other_data, $params); 163 164 // Test that the the second file uses different storage handle from 165 // the first file. 166 $first_handle = $first_file->getStorageHandle(); 167 $second_handle = $second_file->getStorageHandle(); 168 169 $this->assertTrue($first_handle != $second_handle); 170 } 171 172 public function testFileStorageUploadSameFile() { 173 $engine = new PhabricatorTestStorageEngine(); 174 175 $data = Filesystem::readRandomCharacters(64); 176 177 $params = array( 178 'name' => 'test.dat', 179 'storageEngines' => array( 180 $engine, 181 ), 182 ); 183 184 $first_file = PhabricatorFile::newFromFileData($data, $params); 185 186 $second_file = PhabricatorFile::newFromFileData($data, $params); 187 188 // Test that the the second file uses the same storage handle as 189 // the first file. 190 $handle = $first_file->getStorageHandle(); 191 $second_handle = $second_file->getStorageHandle(); 192 193 $this->assertEqual($handle, $second_handle); 194 } 195 196 public function testFileStorageDelete() { 197 $engine = new PhabricatorTestStorageEngine(); 198 199 $data = Filesystem::readRandomCharacters(64); 200 201 $params = array( 202 'name' => 'test.dat', 203 'storageEngines' => array( 204 $engine, 205 ), 206 ); 207 208 $file = PhabricatorFile::newFromFileData($data, $params); 209 $handle = $file->getStorageHandle(); 210 $file->delete(); 211 212 $caught = null; 213 try { 214 $engine->readFile($handle); 215 } catch (Exception $ex) { 216 $caught = $ex; 217 } 218 219 $this->assertTrue($caught instanceof Exception); 220 } 221 222 public function testFileStorageDeleteSharedHandle() { 223 $engine = new PhabricatorTestStorageEngine(); 224 225 $data = Filesystem::readRandomCharacters(64); 226 227 $params = array( 228 'name' => 'test.dat', 229 'storageEngines' => array( 230 $engine, 231 ), 232 ); 233 234 $first_file = PhabricatorFile::newFromFileData($data, $params); 235 $second_file = PhabricatorFile::newFromFileData($data, $params); 236 $first_file->delete(); 237 238 $this->assertEqual($data, $second_file->loadFileData()); 239 } 240 241 public function testReadWriteTtlFiles() { 242 $engine = new PhabricatorTestStorageEngine(); 243 244 $data = Filesystem::readRandomCharacters(64); 245 246 $ttl = (time() + 60 * 60 * 24); 247 248 $params = array( 249 'name' => 'test.dat', 250 'ttl' => ($ttl), 251 'storageEngines' => array( 252 $engine, 253 ), 254 ); 255 256 $file = PhabricatorFile::newFromFileData($data, $params); 257 $this->assertEqual($ttl, $file->getTTL()); 258 } 259 260 public function testFileTransformDelete() { 261 // We want to test that a file deletes all its inbound transformation 262 // records and outbound transformed derivatives when it is deleted. 263 264 // First, we create a chain of transforms, A -> B -> C. 265 266 $engine = new PhabricatorTestStorageEngine(); 267 268 $params = array( 269 'name' => 'test.txt', 270 'storageEngines' => array( 271 $engine, 272 ), 273 ); 274 275 $a = PhabricatorFile::newFromFileData('a', $params); 276 $b = PhabricatorFile::newFromFileData('b', $params); 277 $c = PhabricatorFile::newFromFileData('c', $params); 278 279 id(new PhabricatorTransformedFile()) 280 ->setOriginalPHID($a->getPHID()) 281 ->setTransform('test:a->b') 282 ->setTransformedPHID($b->getPHID()) 283 ->save(); 284 285 id(new PhabricatorTransformedFile()) 286 ->setOriginalPHID($b->getPHID()) 287 ->setTransform('test:b->c') 288 ->setTransformedPHID($c->getPHID()) 289 ->save(); 290 291 // Now, verify that A -> B and B -> C exist. 292 293 $xform_a = id(new PhabricatorFileQuery()) 294 ->setViewer(PhabricatorUser::getOmnipotentUser()) 295 ->withTransforms( 296 array( 297 array( 298 'originalPHID' => $a->getPHID(), 299 'transform' => true, 300 ), 301 )) 302 ->execute(); 303 304 $this->assertEqual(1, count($xform_a)); 305 $this->assertEqual($b->getPHID(), head($xform_a)->getPHID()); 306 307 $xform_b = id(new PhabricatorFileQuery()) 308 ->setViewer(PhabricatorUser::getOmnipotentUser()) 309 ->withTransforms( 310 array( 311 array( 312 'originalPHID' => $b->getPHID(), 313 'transform' => true, 314 ), 315 )) 316 ->execute(); 317 318 $this->assertEqual(1, count($xform_b)); 319 $this->assertEqual($c->getPHID(), head($xform_b)->getPHID()); 320 321 // Delete "B". 322 323 $b->delete(); 324 325 // Now, verify that the A -> B and B -> C links are gone. 326 327 $xform_a = id(new PhabricatorFileQuery()) 328 ->setViewer(PhabricatorUser::getOmnipotentUser()) 329 ->withTransforms( 330 array( 331 array( 332 'originalPHID' => $a->getPHID(), 333 'transform' => true, 334 ), 335 )) 336 ->execute(); 337 338 $this->assertEqual(0, count($xform_a)); 339 340 $xform_b = id(new PhabricatorFileQuery()) 341 ->setViewer(PhabricatorUser::getOmnipotentUser()) 342 ->withTransforms( 343 array( 344 array( 345 'originalPHID' => $b->getPHID(), 346 'transform' => true, 347 ), 348 )) 349 ->execute(); 350 351 $this->assertEqual(0, count($xform_b)); 352 353 // Also verify that C has been deleted. 354 355 $alternate_c = id(new PhabricatorFileQuery()) 356 ->setViewer(PhabricatorUser::getOmnipotentUser()) 357 ->withPHIDs(array($c->getPHID())) 358 ->execute(); 359 360 $this->assertEqual(array(), $alternate_c); 361 } 362 363 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |