12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846 |
- <?php
- function file_test_validator($file, $errors) {
- return $errors;
- }
- function file_test_file_scan_callback($filepath = NULL) {
- $files = &drupal_static(__FUNCTION__, array());
- if (isset($filepath)) {
- $files[] = $filepath;
- }
- else {
- return $files;
- }
- }
- function file_test_file_scan_callback_reset() {
- drupal_static_reset('file_test_file_scan_callback');
- }
- class FileTestCase extends DrupalWebTestCase {
-
- function assertFileUnchanged($before, $after) {
- $this->assertEqual($before->fid, $after->fid, format_string('File id is the same: %file1 == %file2.', array('%file1' => $before->fid, '%file2' => $after->fid)), 'File unchanged');
- $this->assertEqual($before->uid, $after->uid, format_string('File owner is the same: %file1 == %file2.', array('%file1' => $before->uid, '%file2' => $after->uid)), 'File unchanged');
- $this->assertEqual($before->filename, $after->filename, format_string('File name is the same: %file1 == %file2.', array('%file1' => $before->filename, '%file2' => $after->filename)), 'File unchanged');
- $this->assertEqual($before->uri, $after->uri, format_string('File path is the same: %file1 == %file2.', array('%file1' => $before->uri, '%file2' => $after->uri)), 'File unchanged');
- $this->assertEqual($before->filemime, $after->filemime, format_string('File MIME type is the same: %file1 == %file2.', array('%file1' => $before->filemime, '%file2' => $after->filemime)), 'File unchanged');
- $this->assertEqual($before->filesize, $after->filesize, format_string('File size is the same: %file1 == %file2.', array('%file1' => $before->filesize, '%file2' => $after->filesize)), 'File unchanged');
- $this->assertEqual($before->status, $after->status, format_string('File status is the same: %file1 == %file2.', array('%file1' => $before->status, '%file2' => $after->status)), 'File unchanged');
- }
-
- function assertDifferentFile($file1, $file2) {
- $this->assertNotEqual($file1->fid, $file2->fid, format_string('Files have different ids: %file1 != %file2.', array('%file1' => $file1->fid, '%file2' => $file2->fid)), 'Different file');
- $this->assertNotEqual($file1->uri, $file2->uri, format_string('Files have different paths: %file1 != %file2.', array('%file1' => $file1->uri, '%file2' => $file2->uri)), 'Different file');
- }
-
- function assertSameFile($file1, $file2) {
- $this->assertEqual($file1->fid, $file2->fid, format_string('Files have the same ids: %file1 == %file2.', array('%file1' => $file1->fid, '%file2-fid' => $file2->fid)), 'Same file');
- $this->assertEqual($file1->uri, $file2->uri, format_string('Files have the same path: %file1 == %file2.', array('%file1' => $file1->uri, '%file2' => $file2->uri)), 'Same file');
- }
-
- function assertFilePermissions($filepath, $expected_mode, $message = NULL) {
-
- clearstatcache();
-
- $actual_mode = fileperms($filepath) & 0777;
-
-
-
-
-
- if (substr(PHP_OS, 0, 3) == 'WIN') {
-
- $expected_mode = $expected_mode & 0700;
-
- $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
- }
- if (!isset($message)) {
- $message = t('Expected file permission to be %expected, actually were %actual.', array('%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)));
- }
- $this->assertEqual($actual_mode, $expected_mode, $message);
- }
-
- function assertDirectoryPermissions($directory, $expected_mode, $message = NULL) {
-
- clearstatcache();
-
- $actual_mode = fileperms($directory) & 0777;
-
-
-
-
-
- if (substr(PHP_OS, 0, 3) == 'WIN') {
-
- $expected_mode = $expected_mode & 0700;
-
- $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
- }
- if (!isset($message)) {
- $message = t('Expected directory permission to be %expected, actually were %actual.', array('%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)));
- }
- $this->assertEqual($actual_mode, $expected_mode, $message);
- }
-
- function createDirectory($path = NULL) {
-
- if (!isset($path)) {
- $path = file_default_scheme() . '://' . $this->randomName();
- }
- $this->assertTrue(drupal_mkdir($path) && is_dir($path), 'Directory was created successfully.');
- return $path;
- }
-
- function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
- if (!isset($filepath)) {
-
-
- $filepath = 'Файл для тестирования ' . $this->randomName();
- }
- if (!isset($scheme)) {
- $scheme = file_default_scheme();
- }
- $filepath = $scheme . '://' . $filepath;
- if (!isset($contents)) {
- $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
- }
- file_put_contents($filepath, $contents);
- $this->assertTrue(is_file($filepath), 'The test file exists on the disk.', 'Create test file');
- $file = new stdClass();
- $file->uri = $filepath;
- $file->filename = drupal_basename($file->uri);
- $file->filemime = 'text/plain';
- $file->uid = 1;
- $file->timestamp = REQUEST_TIME;
- $file->filesize = filesize($file->uri);
- $file->status = 0;
-
-
- $this->assertNotIdentical(drupal_write_record('file_managed', $file), FALSE, 'The file was added to the database.', 'Create test file');
- return $file;
- }
- }
- class FileHookTestCase extends FileTestCase {
- function setUp() {
-
- parent::setUp('file_test');
-
- file_test_reset();
- }
-
- function assertFileHooksCalled($expected) {
-
- $actual = array_keys(array_filter(file_test_get_all_calls()));
-
- $uncalled = array_diff($expected, $actual);
- if (count($uncalled)) {
- $this->assertTrue(FALSE, format_string('Expected hooks %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled))));
- }
- else {
- $this->assertTrue(TRUE, format_string('All the expected hooks were called: %expected', array('%expected' => empty($expected) ? t('(none)') : implode(', ', $expected))));
- }
-
- $unexpected = array_diff($actual, $expected);
- if (count($unexpected)) {
- $this->assertTrue(FALSE, format_string('Unexpected hooks were called: %unexpected.', array('%unexpected' => empty($unexpected) ? t('(none)') : implode(', ', $unexpected))));
- }
- else {
- $this->assertTrue(TRUE, 'No unexpected hooks were called.');
- }
- }
-
- function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {
- $actual_count = count(file_test_get_calls($hook));
- if (!isset($message)) {
- if ($actual_count == $expected_count) {
- $message = format_string('hook_file_@name was called correctly.', array('@name' => $hook));
- }
- elseif ($expected_count == 0) {
- $message = format_plural($actual_count, 'hook_file_@name was not expected to be called but was actually called once.', 'hook_file_@name was not expected to be called but was actually called @count times.', array('@name' => $hook, '@count' => $actual_count));
- }
- else {
- $message = format_string('hook_file_@name was expected to be called %expected times but was called %actual times.', array('@name' => $hook, '%expected' => $expected_count, '%actual' => $actual_count));
- }
- }
- $this->assertEqual($actual_count, $expected_count, $message);
- }
- }
- class FileSpaceUsedTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File space used tests',
- 'description' => 'Tests the file_space_used() function.',
- 'group' => 'File API',
- );
- }
- function setUp() {
- parent::setUp();
-
- $file = array('uid' => 2, 'uri' => 'public://example1.txt', 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT);
- drupal_write_record('file_managed', $file);
- $file = array('uid' => 2, 'uri' => 'public://example2.txt', 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT);
- drupal_write_record('file_managed', $file);
- $file = array('uid' => 3, 'uri' => 'public://example3.txt', 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT);
- drupal_write_record('file_managed', $file);
- $file = array('uid' => 3, 'uri' => 'public://example4.txt', 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT);
- drupal_write_record('file_managed', $file);
-
- $file = array('uid' => 2, 'uri' => 'public://example5.txt', 'filesize' => 1, 'status' => 0);
- drupal_write_record('file_managed', $file);
- $file = array('uid' => 3, 'uri' => 'public://example6.txt', 'filesize' => 3, 'status' => 0);
- drupal_write_record('file_managed', $file);
- }
-
- function testFileSpaceUsed() {
-
- $this->assertEqual(file_space_used(2), 70);
- $this->assertEqual(file_space_used(3), 300);
- $this->assertEqual(file_space_used(), 370);
-
- $this->assertEqual(file_space_used(NULL, 0), 4);
- $this->assertEqual(file_space_used(NULL, FILE_STATUS_PERMANENT), 370);
-
- $this->assertEqual(file_space_used(1, 0), 0);
- $this->assertEqual(file_space_used(1, FILE_STATUS_PERMANENT), 0);
- $this->assertEqual(file_space_used(2, 0), 1);
- $this->assertEqual(file_space_used(2, FILE_STATUS_PERMANENT), 70);
- $this->assertEqual(file_space_used(3, 0), 3);
- $this->assertEqual(file_space_used(3, FILE_STATUS_PERMANENT), 300);
- }
- }
- class FileValidatorTest extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File validator tests',
- 'description' => 'Tests the functions used to validate uploaded files.',
- 'group' => 'File API',
- );
- }
- function setUp() {
- parent::setUp();
- $this->image = new stdClass();
- $this->image->uri = 'misc/druplicon.png';
- $this->image->filename = drupal_basename($this->image->uri);
- $this->non_image = new stdClass();
- $this->non_image->uri = 'misc/jquery.js';
- $this->non_image->filename = drupal_basename($this->non_image->uri);
- }
-
- function testFileValidateExtensions() {
- $file = new stdClass();
- $file->filename = 'asdf.txt';
- $errors = file_validate_extensions($file, 'asdf txt pork');
- $this->assertEqual(count($errors), 0, 'Valid extension accepted.', 'File');
- $file->filename = 'asdf.txt';
- $errors = file_validate_extensions($file, 'exe png');
- $this->assertEqual(count($errors), 1, 'Invalid extension blocked.', 'File');
- }
-
- function testFileValidateIsImage() {
- $this->assertTrue(file_exists($this->image->uri), 'The image being tested exists.', 'File');
- $errors = file_validate_is_image($this->image);
- $this->assertEqual(count($errors), 0, 'No error reported for our image file.', 'File');
- $this->assertTrue(file_exists($this->non_image->uri), 'The non-image being tested exists.', 'File');
- $errors = file_validate_is_image($this->non_image);
- $this->assertEqual(count($errors), 1, 'An error reported for our non-image file.', 'File');
- }
-
- function testFileValidateImageResolution() {
-
- $errors = file_validate_image_resolution($this->non_image);
- $this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File');
- $errors = file_validate_image_resolution($this->non_image, '50x50', '100x100');
- $this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File');
-
- $errors = file_validate_image_resolution($this->image);
- $this->assertEqual(count($errors), 0, 'No errors for an image when there is no minimum or maximum resolution.', 'File');
- $errors = file_validate_image_resolution($this->image, 0, '200x1');
- $this->assertEqual(count($errors), 1, 'Got an error for an image that was not wide enough.', 'File');
- $errors = file_validate_image_resolution($this->image, 0, '1x200');
- $this->assertEqual(count($errors), 1, 'Got an error for an image that was not tall enough.', 'File');
- $errors = file_validate_image_resolution($this->image, 0, '200x200');
- $this->assertEqual(count($errors), 1, 'Small images report an error.', 'File');
-
- if (image_get_toolkit()) {
-
- copy('misc/druplicon.png', 'temporary://druplicon.png');
- $this->image->uri = 'temporary://druplicon.png';
- $errors = file_validate_image_resolution($this->image, '10x5');
- $this->assertEqual(count($errors), 0, 'No errors should be reported when an oversized image can be scaled down.', 'File');
- $info = image_get_info($this->image->uri);
- $this->assertTrue($info['width'] <= 10, 'Image scaled to correct width.', 'File');
- $this->assertTrue($info['height'] <= 5, 'Image scaled to correct height.', 'File');
- drupal_unlink('temporary://druplicon.png');
- }
- else {
-
- $errors = file_validate_image_resolution($this->image, '5x10');
- $this->assertEqual(count($errors), 1, 'Oversize images that cannot be scaled get an error.', 'File');
- }
- }
-
- function testFileValidateNameLength() {
-
- $file = new stdClass();
-
- $file->filename = str_repeat('x', 240);
- $this->assertEqual(strlen($file->filename), 240);
- $errors = file_validate_name_length($file);
- $this->assertEqual(count($errors), 0, 'No errors reported for 240 length filename.', 'File');
-
- $file->filename = str_repeat('x', 241);
- $errors = file_validate_name_length($file);
- $this->assertEqual(count($errors), 1, 'An error reported for 241 length filename.', 'File');
-
- $file->filename = '';
- $errors = file_validate_name_length($file);
- $this->assertEqual(count($errors), 1, 'An error reported for 0 length filename.', 'File');
- }
-
- function testFileValidateSize() {
-
- $file = new stdClass();
- $file->filesize = 1000;
- $errors = file_validate_size($file, 0, 0);
- $this->assertEqual(count($errors), 0, 'No limits means no errors.', 'File');
- $errors = file_validate_size($file, 1, 0);
- $this->assertEqual(count($errors), 1, 'Error for the file being over the limit.', 'File');
- $errors = file_validate_size($file, 0, 1);
- $this->assertEqual(count($errors), 1, 'Error for the user being over their limit.', 'File');
- $errors = file_validate_size($file, 1, 1);
- $this->assertEqual(count($errors), 2, 'Errors for both the file and their limit.', 'File');
- }
- }
- class FileUnmanagedSaveDataTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Unmanaged file save data',
- 'description' => 'Tests the unmanaged file save data function.',
- 'group' => 'File API',
- );
- }
-
- function testFileSaveData() {
- $contents = $this->randomName(8);
-
- $filepath = file_unmanaged_save_data($contents);
- $this->assertTrue($filepath, 'Unnamed file saved correctly.');
- $this->assertEqual(file_uri_scheme($filepath), file_default_scheme(), "File was placed in Drupal's files directory.");
- $this->assertEqual($contents, file_get_contents($filepath), 'Contents of the file are correct.');
-
- $filepath = file_unmanaged_save_data($contents, 'public://asdf.txt', FILE_EXISTS_REPLACE);
- $this->assertTrue($filepath, 'Unnamed file saved correctly.');
- $this->assertEqual('asdf.txt', drupal_basename($filepath), 'File was named correctly.');
- $this->assertEqual($contents, file_get_contents($filepath), 'Contents of the file are correct.');
- $this->assertFilePermissions($filepath, variable_get('file_chmod_file', 0664));
- }
- }
- class RemoteFileUnmanagedSaveDataTest extends FileUnmanagedSaveDataTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileSaveUploadTest extends FileHookTestCase {
-
- protected $image;
-
- protected $phpfile;
-
- protected $maxFidBefore;
- public static function getInfo() {
- return array(
- 'name' => 'File uploading',
- 'description' => 'Tests the file uploading functions.',
- 'group' => 'File API',
- );
- }
- function setUp() {
- parent::setUp();
- $account = $this->drupalCreateUser(array('access content'));
- $this->drupalLogin($account);
- $image_files = $this->drupalGetTestFiles('image');
- $this->image = current($image_files);
- list(, $this->image_extension) = explode('.', $this->image->filename);
- $this->assertTrue(is_file($this->image->uri), "The image file we're going to upload exists.");
- $this->phpfile = current($this->drupalGetTestFiles('php'));
- $this->assertTrue(is_file($this->phpfile->uri), "The PHP file we're going to upload exists.");
- $this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField();
-
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_REPLACE,
- 'files[file_test_upload]' => drupal_realpath($this->image->uri),
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
-
- $this->assertFileHooksCalled(array('validate', 'insert'));
- file_test_reset();
- }
-
- function testNormal() {
- $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField();
- $this->assertTrue($max_fid_after > $this->maxFidBefore, 'A new file was created.');
- $file1 = file_load($max_fid_after);
- $this->assertTrue($file1, 'Loaded the file.');
-
- $this->assertEqual(substr($file1->filemime, 0, 5), 'image', 'A MIME type was set.');
-
- file_test_reset();
-
- $max_fid_before = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField();
- $image2 = current($this->drupalGetTestFiles('image'));
- $edit = array('files[file_test_upload]' => drupal_realpath($image2->uri));
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertRaw(t('You WIN!'));
- $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField();
-
- $this->assertFileHooksCalled(array('validate', 'insert'));
- $file2 = file_load($max_fid_after);
- $this->assertTrue($file2);
-
- $this->assertEqual(substr($file2->filemime, 0, 5), 'image', 'A MIME type was set.');
-
- $files = file_load_multiple(array($file1->fid, $file2->fid));
- $this->assertTrue(isset($files[$file1->fid]), 'File was loaded successfully');
- $this->assertTrue(isset($files[$file2->fid]), 'File was loaded successfully');
-
- $image3 = current($this->drupalGetTestFiles('image'));
- $image3_realpath = drupal_realpath($image3->uri);
- $dir = $this->randomName();
- $edit = array(
- 'files[file_test_upload]' => $image3_realpath,
- 'file_subdir' => $dir,
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertRaw(t('You WIN!'));
- $this->assertTrue(is_file('temporary://' . $dir . '/' . trim(drupal_basename($image3_realpath))));
-
- $this->assertFalse(file_load_multiple(), 'No files were loaded.');
- }
-
- function testHandleExtension() {
-
-
-
-
- $extensions = 'foo';
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_REPLACE,
- 'files[file_test_upload]' => drupal_realpath($this->image->uri),
- 'extensions' => $extensions,
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $message = t('Only files with the following extensions are allowed:') . ' <em class="placeholder">' . $extensions . '</em>';
- $this->assertRaw($message, 'Cannot upload a disallowed extension');
- $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');
-
- $this->assertFileHooksCalled(array('validate'));
-
- file_test_reset();
- $extensions = 'foo ' . $this->image_extension;
-
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_REPLACE,
- 'files[file_test_upload]' => drupal_realpath($this->image->uri),
- 'extensions' => $extensions,
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertNoRaw(t('Only files with the following extensions are allowed:'), 'Can upload an allowed extension.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'load', 'update'));
-
- file_test_reset();
-
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_REPLACE,
- 'files[file_test_upload]' => drupal_realpath($this->image->uri),
- 'allow_all_extensions' => TRUE,
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertNoRaw(t('Only files with the following extensions are allowed:'), 'Can upload any extension.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'load', 'update'));
- }
-
- function testHandleDangerousFile() {
-
-
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_REPLACE,
- 'files[file_test_upload]' => drupal_realpath($this->phpfile->uri),
- 'is_image_file' => FALSE,
- 'extensions' => 'php',
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $message = t('For security reasons, your upload has been renamed to') . ' <em class="placeholder">' . $this->phpfile->filename . '.txt' . '</em>';
- $this->assertRaw($message, 'Dangerous file was renamed.');
- $this->assertRaw(t('File MIME type is text/plain.'), "Dangerous file's MIME type was changed.");
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'insert'));
-
-
- variable_set('allow_insecure_uploads', 1);
-
- file_test_reset();
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.');
- $this->assertRaw(t('File name is !filename', array('!filename' => $this->phpfile->filename)), 'Dangerous file was not renamed when insecure uploads is TRUE.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'insert'));
-
- variable_set('allow_insecure_uploads', 0);
- }
-
- function testHandleFileMunge() {
-
- variable_set('allow_insecure_uploads', 0);
- $this->image = file_move($this->image, $this->image->uri . '.foo.' . $this->image_extension);
-
- file_test_reset();
- $extensions = $this->image_extension;
- $edit = array(
- 'files[file_test_upload]' => drupal_realpath($this->image->uri),
- 'extensions' => $extensions,
- );
- $munged_filename = $this->image->filename;
- $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
- $munged_filename .= '_.' . $this->image_extension;
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
- $this->assertRaw(t('File name is !filename', array('!filename' => $munged_filename)), 'File was successfully munged.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'insert'));
-
-
- file_test_reset();
- $edit = array(
- 'files[file_test_upload]' => drupal_realpath($this->image->uri),
- 'allow_all_extensions' => TRUE,
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.');
- $this->assertRaw(t('File name is !filename', array('!filename' => $this->image->filename)), 'File was not munged when allowing any extension.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'insert'));
- }
-
- function testExistingRename() {
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_RENAME,
- 'files[file_test_upload]' => drupal_realpath($this->image->uri)
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'insert'));
- }
-
- function testExistingReplace() {
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_REPLACE,
- 'files[file_test_upload]' => drupal_realpath($this->image->uri)
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertRaw(t('You WIN!'), 'Found the success message.');
-
- $this->assertFileHooksCalled(array('validate', 'load', 'update'));
- }
-
- function testExistingError() {
- $edit = array(
- 'file_test_replace' => FILE_EXISTS_ERROR,
- 'files[file_test_upload]' => drupal_realpath($this->image->uri)
- );
- $this->drupalPost('file-test/upload', $edit, t('Submit'));
- $this->assertResponse(200, 'Received a 200 response for posted test file.');
- $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');
-
- $this->assertFileHooksCalled(array());
- }
-
- function testNoUpload() {
- $this->drupalPost('file-test/upload', array(), t('Submit'));
- $this->assertNoRaw(t('Epic upload FAIL!'), 'Failure message not found.');
- }
- }
- class RemoteFileSaveUploadTest extends FileSaveUploadTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileDirectoryTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File paths and directories',
- 'description' => 'Tests operations dealing with directories.',
- 'group' => 'File API',
- );
- }
-
- function testFileCheckDirectoryHandling() {
-
- $directory = file_default_scheme() . '://' . $this->randomName() . '/' . $this->randomName();
- $this->assertFalse(is_dir($directory), 'Directory does not exist prior to testing.');
-
- $this->assertFalse(file_prepare_directory($directory, 0), 'Error reported for non-existing directory.', 'File');
-
- $this->assertTrue(file_prepare_directory($directory, FILE_CREATE_DIRECTORY), 'No error reported when creating a new directory.', 'File');
-
- $this->assertTrue(is_dir($directory), 'Directory actually exists.', 'File');
- if (substr(PHP_OS, 0, 3) != 'WIN') {
-
-
-
-
-
- @drupal_chmod($directory, 0444);
- $this->assertFalse(file_prepare_directory($directory, 0), 'Error reported for a non-writeable directory.', 'File');
-
- $this->assertTrue(file_prepare_directory($directory, FILE_MODIFY_PERMISSIONS), 'No error reported when making directory writeable.', 'File');
- }
-
- $this->assertDirectoryPermissions($directory, variable_get('file_chmod_directory', 0775));
-
- @drupal_unlink(file_default_scheme() . '://.htaccess');
- $this->assertFalse(is_file(file_default_scheme() . '://.htaccess'), 'Successfully removed the .htaccess file in the files directory.', 'File');
- file_ensure_htaccess();
- $this->assertTrue(is_file(file_default_scheme() . '://.htaccess'), 'Successfully re-created the .htaccess file in the files directory.', 'File');
-
- $file = file_get_contents(file_default_scheme() . '://.htaccess');
- $this->assertEqual($file, file_htaccess_lines(FALSE), 'The .htaccess file contains the proper content.', 'File');
- }
-
- function testFileCreateNewFilepath() {
-
-
- $basename = 'xyz.txt';
- $directory = 'misc';
- $original = $directory . '/' . $basename;
- $path = file_create_filename($basename, $directory);
- $this->assertEqual($path, $original, format_string('New filepath %new equals %original.', array('%new' => $path, '%original' => $original)), 'File');
-
- $basename = 'druplicon.png';
- $original = $directory . '/' . $basename;
- $expected = $directory . '/druplicon_0.png';
- $path = file_create_filename($basename, $directory);
- $this->assertEqual($path, $expected, format_string('Creating a new filepath from %original equals %new.', array('%new' => $path, '%original' => $original)), 'File');
- try {
- $filename = "a\xFFtest\x80€.txt";
- file_create_filename($filename, $directory);
- $this->fail('Expected exception not thrown');
- }
- catch (RuntimeException $e) {
- $this->assertEqual("Invalid filename '$filename'", $e->getMessage());
- }
-
- }
-
- function testFileDestination() {
-
- $destination = 'misc/xyz.txt';
- $path = file_destination($destination, FILE_EXISTS_REPLACE);
- $this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FILE_EXISTS_REPLACE.', 'File');
- $path = file_destination($destination, FILE_EXISTS_RENAME);
- $this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FILE_EXISTS_RENAME.', 'File');
- $path = file_destination($destination, FILE_EXISTS_ERROR);
- $this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FILE_EXISTS_ERROR.', 'File');
- $destination = 'misc/druplicon.png';
- $path = file_destination($destination, FILE_EXISTS_REPLACE);
- $this->assertEqual($path, $destination, 'Existing filepath destination remains the same with FILE_EXISTS_REPLACE.', 'File');
- $path = file_destination($destination, FILE_EXISTS_RENAME);
- $this->assertNotEqual($path, $destination, 'A new filepath destination is created when filepath destination already exists with FILE_EXISTS_RENAME.', 'File');
- $path = file_destination($destination, FILE_EXISTS_ERROR);
- $this->assertEqual($path, FALSE, 'An error is returned when filepath destination already exists with FILE_EXISTS_ERROR.', 'File');
- try {
- file_destination("core/misc/a\xFFtest\x80€.txt", FILE_EXISTS_REPLACE);
- $this->fail('Expected exception not thrown');
- }
- catch (RuntimeException $e) {
- $this->assertEqual("Invalid filename 'a\xFFtest\x80€.txt'", $e->getMessage());
- }
- }
-
- function testFileDirectoryTemp() {
-
- variable_set('file_temporary_path', '');
- $tmp_directory = file_directory_temp();
- $this->assertEqual(empty($tmp_directory), FALSE, 'file_directory_temp() returned a non-empty value.');
- $setting = variable_get('file_temporary_path', '');
- $this->assertEqual($setting, $tmp_directory, "The 'file_temporary_path' variable has the same value that file_directory_temp() returned.");
- }
- }
- class RemoteFileDirectoryTest extends FileDirectoryTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileScanDirectoryTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File scan directory',
- 'description' => 'Tests the file_scan_directory() function.',
- 'group' => 'File API',
- );
- }
- function setUp() {
- parent::setUp();
- $this->path = drupal_get_path('module', 'simpletest') . '/files';
- }
-
- function testReturn() {
-
-
- $all_files = file_scan_directory($this->path, '/^javascript-/');
- ksort($all_files);
- $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
-
- $file = reset($all_files);
- $this->assertEqual(key($all_files), $file->uri, 'Correct array key was used for the first returned file.');
- $this->assertEqual($file->uri, $this->path . '/javascript-1.txt', 'First file name was set correctly.');
- $this->assertEqual($file->filename, 'javascript-1.txt', 'First basename was set correctly');
- $this->assertEqual($file->name, 'javascript-1', 'First name was set correctly.');
-
- $file = next($all_files);
- $this->assertEqual(key($all_files), $file->uri, 'Correct array key was used for the second returned file.');
- $this->assertEqual($file->uri, $this->path . '/javascript-2.script', 'Second file name was set correctly.');
- $this->assertEqual($file->filename, 'javascript-2.script', 'Second basename was set correctly');
- $this->assertEqual($file->name, 'javascript-2', 'Second name was set correctly.');
- }
-
- function testOptionCallback() {
-
- $all_files = file_scan_directory($this->path, '/^NONEXISTINGFILENAME/', array('callback' => 'file_test_file_scan_callback'));
- $this->assertEqual(0, count($all_files), 'No files were found.');
- $results = file_test_file_scan_callback();
- file_test_file_scan_callback_reset();
- $this->assertEqual(0, count($results), 'No files were passed to the callback.');
-
-
- $all_files = file_scan_directory($this->path, '/^javascript-/', array('callback' => 'file_test_file_scan_callback'));
- $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
- $results = file_test_file_scan_callback();
- file_test_file_scan_callback_reset();
- $this->assertEqual(2, count($results), 'Files were passed to the callback.');
- }
-
- function testOptionNoMask() {
-
- $all_files = file_scan_directory($this->path, '/^javascript-/');
- $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
-
- $filtered_files = file_scan_directory($this->path, '/^javascript-/', array('nomask' => '/.script$/'));
- $this->assertEqual(1, count($filtered_files), 'Filtered correctly.');
- }
-
- function testOptionKey() {
-
- $expected = array($this->path . '/javascript-1.txt', $this->path . '/javascript-2.script');
- $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array('key' => 'filepath')));
- sort($actual);
- $this->assertEqual($expected, $actual, 'Returned the correct values for the filename key.');
-
- $expected = array('javascript-1.txt', 'javascript-2.script');
- $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array('key' => 'filename')));
- sort($actual);
- $this->assertEqual($expected, $actual, 'Returned the correct values for the basename key.');
-
- $expected = array('javascript-1', 'javascript-2');
- $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array('key' => 'name')));
- sort($actual);
- $this->assertEqual($expected, $actual, 'Returned the correct values for the name key.');
-
- $expected = array($this->path . '/javascript-1.txt', $this->path . '/javascript-2.script');
- $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array('key' => 'INVALID')));
- sort($actual);
- $this->assertEqual($expected, $actual, 'An invalid key defaulted back to the default.');
- }
-
- function testOptionRecurse() {
- $files = file_scan_directory(drupal_get_path('module', 'simpletest'), '/^javascript-/', array('recurse' => FALSE));
- $this->assertTrue(empty($files), "Without recursion couldn't find javascript files.");
- $files = file_scan_directory(drupal_get_path('module', 'simpletest'), '/^javascript-/', array('recurse' => TRUE));
- $this->assertEqual(2, count($files), 'With recursion we found the expected javascript files.');
- }
-
- function testOptionMinDepth() {
- $files = file_scan_directory($this->path, '/^javascript-/', array('min_depth' => 0));
- $this->assertEqual(2, count($files), 'No minimum-depth gets files in current directory.');
- $files = file_scan_directory($this->path, '/^javascript-/', array('min_depth' => 1));
- $this->assertTrue(empty($files), "Minimum-depth of 1 successfully excludes files from current directory.");
- }
- }
- class RemoteFileScanDirectoryTest extends FileScanDirectoryTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileUnmanagedDeleteTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Unmanaged file delete',
- 'description' => 'Tests the unmanaged file delete function.',
- 'group' => 'File API',
- );
- }
-
- function testNormal() {
-
- $file = $this->createFile();
-
- $this->assertTrue(file_unmanaged_delete($file->uri), 'Deleted worked.');
- $this->assertFalse(file_exists($file->uri), 'Test file has actually been deleted.');
- }
-
- function testMissing() {
-
- $this->assertTrue(file_unmanaged_delete(file_default_scheme() . '/' . $this->randomName()), 'Returns true when deleting a non-existent file.');
- }
-
- function testDirectory() {
-
- $directory = $this->createDirectory();
-
- $this->assertFalse(file_unmanaged_delete($directory), 'Could not delete the delete directory.');
- $this->assertTrue(file_exists($directory), 'Directory has not been deleted.');
- }
- }
- class RemoteFileUnmanagedDeleteTest extends FileUnmanagedDeleteTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileUnmanagedDeleteRecursiveTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Unmanaged recursive file delete',
- 'description' => 'Tests the unmanaged file delete recursive function.',
- 'group' => 'File API',
- );
- }
-
- function testSingleFile() {
-
- $filepath = file_default_scheme() . '://' . $this->randomName();
- file_put_contents($filepath, '');
-
- $this->assertTrue(file_unmanaged_delete_recursive($filepath), 'Function reported success.');
- $this->assertFalse(file_exists($filepath), 'Test file has been deleted.');
- }
-
- function testEmptyDirectory() {
-
- $directory = $this->createDirectory();
-
- $this->assertTrue(file_unmanaged_delete_recursive($directory), 'Function reported success.');
- $this->assertFalse(file_exists($directory), 'Directory has been deleted.');
- }
-
- function testDirectory() {
-
- $directory = $this->createDirectory();
- $filepathA = $directory . '/A';
- $filepathB = $directory . '/B';
- file_put_contents($filepathA, '');
- file_put_contents($filepathB, '');
-
- $this->assertTrue(file_unmanaged_delete_recursive($directory), 'Function reported success.');
- $this->assertFalse(file_exists($filepathA), 'Test file A has been deleted.');
- $this->assertFalse(file_exists($filepathB), 'Test file B has been deleted.');
- $this->assertFalse(file_exists($directory), 'Directory has been deleted.');
- }
-
- function testSubDirectory() {
-
- $directory = $this->createDirectory();
- $subdirectory = $this->createDirectory($directory . '/sub');
- $filepathA = $directory . '/A';
- $filepathB = $subdirectory . '/B';
- file_put_contents($filepathA, '');
- file_put_contents($filepathB, '');
-
- $this->assertTrue(file_unmanaged_delete_recursive($directory), 'Function reported success.');
- $this->assertFalse(file_exists($filepathA), 'Test file A has been deleted.');
- $this->assertFalse(file_exists($filepathB), 'Test file B has been deleted.');
- $this->assertFalse(file_exists($subdirectory), 'Subdirectory has been deleted.');
- $this->assertFalse(file_exists($directory), 'Directory has been deleted.');
- }
- }
- class RemoteFileUnmanagedDeleteRecursiveTest extends FileUnmanagedDeleteRecursiveTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileUnmanagedMoveTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Unmanaged file moving',
- 'description' => 'Tests the unmanaged file move function.',
- 'group' => 'File API',
- );
- }
-
- function testNormal() {
-
- $file = $this->createFile();
-
- $desired_filepath = 'public://' . $this->randomName();
- $new_filepath = file_unmanaged_move($file->uri, $desired_filepath, FILE_EXISTS_ERROR);
- $this->assertTrue($new_filepath, 'Move was successful.');
- $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
- $this->assertTrue(file_exists($new_filepath), 'File exists at the new location.');
- $this->assertFalse(file_exists($file->uri), 'No file remains at the old location.');
- $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
-
- $desired_filepath = 'public://' . $this->randomName();
- $this->assertTrue(file_exists($new_filepath), 'File exists before moving.');
- $this->assertTrue(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
- $newer_filepath = file_unmanaged_move($new_filepath, $desired_filepath, FILE_EXISTS_RENAME);
- $this->assertTrue($newer_filepath, 'Move was successful.');
- $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
- $this->assertTrue(file_exists($newer_filepath), 'File exists at the new location.');
- $this->assertFalse(file_exists($new_filepath), 'No file remains at the old location.');
- $this->assertFilePermissions($newer_filepath, variable_get('file_chmod_file', 0664));
-
-
- }
-
- function testMissing() {
-
- $new_filepath = file_unmanaged_move($this->randomName(), $this->randomName());
- $this->assertFalse($new_filepath, 'Moving a missing file fails.');
- }
-
- function testOverwriteSelf() {
-
- $file = $this->createFile();
-
- $new_filepath = file_unmanaged_move($file->uri, $file->uri, FILE_EXISTS_REPLACE);
- $this->assertFalse($new_filepath, 'Moving onto itself without renaming fails.');
- $this->assertTrue(file_exists($file->uri), 'File exists after moving onto itself.');
-
- $new_filepath = file_unmanaged_move($file->uri, $file->uri, FILE_EXISTS_RENAME);
- $this->assertTrue($new_filepath, 'Moving onto itself with renaming works.');
- $this->assertFalse(file_exists($file->uri), 'Original file has been removed.');
- $this->assertTrue(file_exists($new_filepath), 'File exists after moving onto itself.');
- }
- }
- class RemoteFileUnmanagedMoveTest extends FileUnmanagedMoveTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileUnmanagedCopyTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Unmanaged file copying',
- 'description' => 'Tests the unmanaged file copy function.',
- 'group' => 'File API',
- );
- }
-
- function testNormal() {
-
- $file = $this->createFile();
-
- $desired_filepath = 'public://' . $this->randomName();
- $new_filepath = file_unmanaged_copy($file->uri, $desired_filepath, FILE_EXISTS_ERROR);
- $this->assertTrue($new_filepath, 'Copy was successful.');
- $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
- $this->assertTrue(file_exists($file->uri), 'Original file remains.');
- $this->assertTrue(file_exists($new_filepath), 'New file exists.');
- $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
-
- $desired_filepath = 'public://' . $this->randomName();
- $this->assertTrue(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
- $newer_filepath = file_unmanaged_copy($file->uri, $desired_filepath, FILE_EXISTS_RENAME);
- $this->assertTrue($newer_filepath, 'Copy was successful.');
- $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
- $this->assertTrue(file_exists($file->uri), 'Original file remains.');
- $this->assertTrue(file_exists($newer_filepath), 'New file exists.');
- $this->assertFilePermissions($newer_filepath, variable_get('file_chmod_file', 0664));
-
-
- }
-
- function testNonExistent() {
-
- $desired_filepath = $this->randomName();
- $this->assertFalse(file_exists($desired_filepath), "Randomly named file doesn't exists.");
- $new_filepath = file_unmanaged_copy($desired_filepath, $this->randomName());
- $this->assertFalse($new_filepath, 'Copying a missing file fails.');
- }
-
- function testOverwriteSelf() {
-
- $file = $this->createFile();
-
- $new_filepath = file_unmanaged_copy($file->uri, $file->uri, FILE_EXISTS_RENAME);
- $this->assertTrue($new_filepath, 'Copying onto itself with renaming works.');
- $this->assertNotEqual($new_filepath, $file->uri, 'Copied file has a new name.');
- $this->assertTrue(file_exists($file->uri), 'Original file exists after copying onto itself.');
- $this->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.');
- $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
-
- $new_filepath = file_unmanaged_copy($file->uri, $file->uri, FILE_EXISTS_ERROR);
- $this->assertFalse($new_filepath, 'Copying onto itself without renaming fails.');
- $this->assertTrue(file_exists($file->uri), 'File exists after copying onto itself.');
-
- $new_filepath = file_unmanaged_copy($file->uri, drupal_dirname($file->uri), FILE_EXISTS_ERROR);
- $this->assertFalse($new_filepath, 'Copying onto itself fails.');
- $this->assertTrue(file_exists($file->uri), 'File exists after copying onto itself.');
-
- $new_filepath = file_unmanaged_copy($file->uri, drupal_dirname($file->uri), FILE_EXISTS_RENAME);
- $this->assertTrue($new_filepath, 'Copying into same directory works.');
- $this->assertNotEqual($new_filepath, $file->uri, 'Copied file has a new name.');
- $this->assertTrue(file_exists($file->uri), 'Original file exists after copying onto itself.');
- $this->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.');
- $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
- }
- }
- class RemoteFileUnmanagedCopyTest extends FileUnmanagedCopyTest {
- public static function getInfo() {
- $info = parent::getInfo();
- $info['group'] = 'File API (remote)';
- return $info;
- }
- function setUp() {
- parent::setUp('file_test');
- variable_set('file_default_scheme', 'dummy-remote');
- }
- }
- class FileDeleteTest extends FileHookTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File delete',
- 'description' => 'Tests the file delete function.',
- 'group' => 'File API',
- );
- }
-
- function testUnused() {
- $file = $this->createFile();
-
- $this->assertTrue(is_file($file->uri), 'File exists.');
- $this->assertIdentical(file_delete($file), TRUE, 'Delete worked.');
- $this->assertFileHooksCalled(array('delete'));
- $this->assertFalse(file_exists($file->uri), 'Test file has actually been deleted.');
- $this->assertFalse(file_load($file->fid), 'File was removed from the database.');
- }
-
- function testInUse() {
- $file = $this->createFile();
- file_usage_add($file, 'testing', 'test', 1);
- file_usage_add($file, 'testing', 'test', 1);
- file_usage_delete($file, 'testing', 'test', 1);
- file_delete($file);
- $usage = file_usage_list($file);
- $this->assertEqual($usage['testing']['test'], array(1 => 1), 'Test file is still in use.');
- $this->assertTrue(file_exists($file->uri), 'File still exists on the disk.');
- $this->assertTrue(file_load($file->fid), 'File still exists in the database.');
-
- file_test_reset();
- file_usage_delete($file, 'testing', 'test', 1);
- file_delete($file);
- $usage = file_usage_list($file);
- $this->assertFileHooksCalled(array('delete'));
- $this->assertTrue(empty($usage), 'File usage data was removed.');
- $this->assertFalse(file_exists($file->uri), 'File has been deleted after its last usage was removed.');
- $this->assertFalse(file_load($file->fid), 'File was removed from the database.');
- }
- }
- class FileMoveTest extends FileHookTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File moving',
- 'description' => 'Tests the file move function.',
- 'group' => 'File API',
- );
- }
-
- function testNormal() {
- $contents = $this->randomName(10);
- $source = $this->createFile(NULL, $contents);
- $desired_filepath = 'public://' . $this->randomName();
-
-
- $result = file_move(clone $source, $desired_filepath, FILE_EXISTS_ERROR);
-
- $this->assertTrue($result, 'File moved successfully.');
- $this->assertFalse(file_exists($source->uri));
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file correctly written.');
-
- $this->assertFileHooksCalled(array('move', 'load', 'update'));
-
- $this->assertEqual($source->fid, $result->fid, format_string("Source file id's' %fid is unchanged after move.", array('%fid' => $source->fid)));
-
-
- $loaded_file = file_load($result->fid, TRUE);
- $this->assertTrue($loaded_file, 'File can be loaded from the database.');
- $this->assertFileUnchanged($result, $loaded_file);
- }
-
- function testExistingRename() {
-
- $contents = $this->randomName(10);
- $source = $this->createFile(NULL, $contents);
- $target = $this->createFile();
- $this->assertDifferentFile($source, $target);
-
-
- $result = file_move(clone $source, $target->uri, FILE_EXISTS_RENAME);
-
- $this->assertTrue($result, 'File moved successfully.');
- $this->assertFalse(file_exists($source->uri));
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file correctly written.');
-
- $this->assertFileHooksCalled(array('move', 'load', 'update'));
-
- $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
-
- $this->assertFileUnchanged($target, file_load($target->fid, TRUE));
-
- $this->assertDifferentFile($target, $result);
-
- $loaded_source = file_load($source->fid, TRUE);
- $this->assertEqual($loaded_source->fid, $result->fid, "Returned file's id matches the source.");
- $this->assertNotEqual($loaded_source->uri, $source->uri, 'Returned file path has changed from the original.');
- }
-
- function testExistingReplace() {
-
- $contents = $this->randomName(10);
- $source = $this->createFile(NULL, $contents);
- $target = $this->createFile();
- $this->assertDifferentFile($source, $target);
-
-
- $result = file_move(clone $source, $target->uri, FILE_EXISTS_REPLACE);
-
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file were overwritten.');
- $this->assertFalse(file_exists($source->uri));
- $this->assertTrue($result, 'File moved successfully.');
-
- $this->assertFileHooksCalled(array('move', 'update', 'delete', 'load'));
-
-
- $loaded_result = file_load($result->fid, TRUE);
- $this->assertFileUnchanged($result, $loaded_result);
-
- $this->assertSameFile($target, $loaded_result);
-
- $this->assertDifferentFile($source, $loaded_result);
- }
-
- function testExistingReplaceSelf() {
-
- $contents = $this->randomName(10);
- $source = $this->createFile(NULL, $contents);
-
-
- $result = file_move(clone $source, $source->uri, FILE_EXISTS_REPLACE);
- $this->assertFalse($result, 'File move failed.');
- $this->assertEqual($contents, file_get_contents($source->uri), 'Contents of file were not altered.');
-
- $this->assertFileHooksCalled(array());
-
-
- $this->assertFileUnchanged($source, file_load($source->fid, TRUE));
- }
-
- function testExistingError() {
- $contents = $this->randomName(10);
- $source = $this->createFile();
- $target = $this->createFile(NULL, $contents);
- $this->assertDifferentFile($source, $target);
-
-
- $result = file_move(clone $source, $target->uri, FILE_EXISTS_ERROR);
-
- $this->assertFalse($result, 'File move failed.');
- $this->assertTrue(file_exists($source->uri));
- $this->assertEqual($contents, file_get_contents($target->uri), 'Contents of file were not altered.');
-
- $this->assertFileHooksCalled(array());
-
-
- $this->assertFileUnchanged($source, file_load($source->fid, TRUE));
- $this->assertFileUnchanged($target, file_load($target->fid, TRUE));
- }
- }
- class FileCopyTest extends FileHookTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File copying',
- 'description' => 'Tests the file copy function.',
- 'group' => 'File API',
- );
- }
-
- function testNormal() {
- $contents = $this->randomName(10);
- $source = $this->createFile(NULL, $contents);
- $desired_uri = 'public://' . $this->randomName();
-
-
- $result = file_copy(clone $source, $desired_uri, FILE_EXISTS_ERROR);
-
- $this->assertTrue($result, 'File copied successfully.');
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file were copied correctly.');
-
- $this->assertFileHooksCalled(array('copy', 'insert'));
- $this->assertDifferentFile($source, $result);
- $this->assertEqual($result->uri, $desired_uri, 'The copied file object has the desired filepath.');
- $this->assertTrue(file_exists($source->uri), 'The original file still exists.');
- $this->assertTrue(file_exists($result->uri), 'The copied file exists.');
-
-
- $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
- }
-
- function testExistingRename() {
-
- $contents = $this->randomName(10);
- $source = $this->createFile(NULL, $contents);
- $target = $this->createFile();
- $this->assertDifferentFile($source, $target);
-
-
- $result = file_copy(clone $source, $target->uri, FILE_EXISTS_RENAME);
-
- $this->assertTrue($result, 'File copied successfully.');
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file were copied correctly.');
- $this->assertNotEqual($result->uri, $source->uri, 'Returned file path has changed from the original.');
-
- $this->assertFileHooksCalled(array('copy', 'insert'));
-
-
- $loaded_source = file_load($source->fid, TRUE);
- $loaded_target = file_load($target->fid, TRUE);
- $loaded_result = file_load($result->fid, TRUE);
-
- $this->assertFileUnchanged($source, $loaded_source);
-
- $this->assertFileUnchanged($result, $loaded_result);
-
- $this->assertDifferentFile($loaded_source, $loaded_target);
- $this->assertDifferentFile($loaded_target, $loaded_result);
- $this->assertDifferentFile($loaded_source, $loaded_result);
- }
-
- function testExistingReplace() {
-
- $contents = $this->randomName(10);
- $source = $this->createFile(NULL, $contents);
- $target = $this->createFile();
- $this->assertDifferentFile($source, $target);
-
-
- $result = file_copy(clone $source, $target->uri, FILE_EXISTS_REPLACE);
-
- $this->assertTrue($result, 'File copied successfully.');
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file were overwritten.');
- $this->assertDifferentFile($source, $result);
-
- $this->assertFileHooksCalled(array('load', 'copy', 'update'));
-
-
- $loaded_source = file_load($source->fid, TRUE);
- $loaded_target = file_load($target->fid, TRUE);
- $loaded_result = file_load($result->fid, TRUE);
-
- $this->assertFileUnchanged($source, $loaded_source);
-
- $this->assertFileUnchanged($result, $loaded_result);
-
- $this->assertFileUnchanged($loaded_target, $loaded_result);
- }
-
- function testExistingError() {
- $contents = $this->randomName(10);
- $source = $this->createFile();
- $target = $this->createFile(NULL, $contents);
- $this->assertDifferentFile($source, $target);
-
-
- $result = file_copy(clone $source, $target->uri, FILE_EXISTS_ERROR);
-
- $this->assertFalse($result, 'File copy failed.');
- $this->assertEqual($contents, file_get_contents($target->uri), 'Contents of file were not altered.');
-
- $this->assertFileHooksCalled(array());
- $this->assertFileUnchanged($source, file_load($source->fid, TRUE));
- $this->assertFileUnchanged($target, file_load($target->fid, TRUE));
- }
- }
- class FileLoadTest extends FileHookTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File loading',
- 'description' => 'Tests the file_load() function.',
- 'group' => 'File API',
- );
- }
-
- function testLoadMissingFid() {
- $this->assertFalse(file_load(-1), "Try to load an invalid fid fails.");
- $this->assertFileHooksCalled(array());
- }
-
- function testLoadMissingFilepath() {
- $files = file_load_multiple(array(), array('uri' => 'foobar://misc/druplicon.png'));
- $this->assertFalse(reset($files), "Try to load a file that doesn't exist in the database fails.");
- $this->assertFileHooksCalled(array());
- }
-
- function testLoadInvalidStatus() {
- $files = file_load_multiple(array(), array('status' => -99));
- $this->assertFalse(reset($files), "Trying to load a file with an invalid status fails.");
- $this->assertFileHooksCalled(array());
- }
-
- function testSingleValues() {
-
- $file = $this->createFile('druplicon.txt', NULL, 'public');
- $by_fid_file = file_load($file->fid);
- $this->assertFileHookCalled('load');
- $this->assertTrue(is_object($by_fid_file), 'file_load() returned an object.');
- $this->assertEqual($by_fid_file->fid, $file->fid, 'Loading by fid got the same fid.', 'File');
- $this->assertEqual($by_fid_file->uri, $file->uri, 'Loading by fid got the correct filepath.', 'File');
- $this->assertEqual($by_fid_file->filename, $file->filename, 'Loading by fid got the correct filename.', 'File');
- $this->assertEqual($by_fid_file->filemime, $file->filemime, 'Loading by fid got the correct MIME type.', 'File');
- $this->assertEqual($by_fid_file->status, $file->status, 'Loading by fid got the correct status.', 'File');
- $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
- }
-
- function testMultiple() {
-
- $file = $this->createFile('druplicon.txt', NULL, 'public');
-
- file_test_reset();
- $by_path_files = file_load_multiple(array(), array('uri' => $file->uri));
- $this->assertFileHookCalled('load');
- $this->assertEqual(1, count($by_path_files), 'file_load_multiple() returned an array of the correct size.');
- $by_path_file = reset($by_path_files);
- $this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
- $this->assertEqual($by_path_file->fid, $file->fid, 'Loading by filepath got the correct fid.', 'File');
-
- file_test_reset();
- $by_fid_files = file_load_multiple(array($file->fid), array());
- $this->assertFileHookCalled('load');
- $this->assertEqual(1, count($by_fid_files), 'file_load_multiple() returned an array of the correct size.');
- $by_fid_file = reset($by_fid_files);
- $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
- $this->assertEqual($by_fid_file->uri, $file->uri, 'Loading by fid got the correct filepath.', 'File');
- }
- }
- class FileSaveTest extends FileHookTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File saving',
- 'description' => 'Tests the file_save() function.',
- 'group' => 'File API',
- );
- }
- function testFileSave() {
-
- $file = array(
- 'uid' => 1,
- 'filename' => 'druplicon.txt',
- 'uri' => 'public://druplicon.txt',
- 'filemime' => 'text/plain',
- 'timestamp' => 1,
- 'status' => FILE_STATUS_PERMANENT,
- );
- $file = (object) $file;
- file_put_contents($file->uri, 'hello world');
-
- $saved_file = file_save($file);
-
- $this->assertFileHooksCalled(array('insert'));
- $this->assertNotNull($saved_file, 'Saving the file should give us back a file object.', 'File');
- $this->assertTrue($saved_file->fid > 0, 'A new file ID is set when saving a new file to the database.', 'File');
- $loaded_file = db_query('SELECT * FROM {file_managed} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
- $this->assertNotNull($loaded_file, 'Record exists in the database.');
- $this->assertEqual($loaded_file->status, $file->status, 'Status was saved correctly.');
- $this->assertEqual($saved_file->filesize, filesize($file->uri), 'File size was set correctly.', 'File');
- $this->assertTrue($saved_file->timestamp > 1, 'File size was set correctly.', 'File');
-
- file_test_reset();
- $saved_file->status = 7;
- $resaved_file = file_save($saved_file);
-
- $this->assertFileHooksCalled(array('load', 'update'));
- $this->assertEqual($resaved_file->fid, $saved_file->fid, 'The file ID of an existing file is not changed when updating the database.', 'File');
- $this->assertTrue($resaved_file->timestamp >= $saved_file->timestamp, "Timestamp didn't go backwards.", 'File');
- $loaded_file = db_query('SELECT * FROM {file_managed} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
- $this->assertNotNull($loaded_file, 'Record still exists in the database.', 'File');
- $this->assertEqual($loaded_file->status, $saved_file->status, 'Status was saved correctly.');
-
-
- $file = (object) array(
- 'uid' => 1,
- 'filename' => 'DRUPLICON.txt',
- 'uri' => 'public://DRUPLICON.txt',
- 'filemime' => 'text/plain',
- 'timestamp' => 1,
- 'status' => FILE_STATUS_PERMANENT,
- );
- file_put_contents($file->uri, 'hello world');
- file_save($file);
- }
- }
- class FileUsageTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File usage',
- 'description' => 'Tests the file usage functions.',
- 'group' => 'File',
- );
- }
-
- function testGetUsage() {
- $file = $this->createFile();
- db_insert('file_usage')
- ->fields(array(
- 'fid' => $file->fid,
- 'module' => 'testing',
- 'type' => 'foo',
- 'id' => 1,
- 'count' => 1
- ))
- ->execute();
- db_insert('file_usage')
- ->fields(array(
- 'fid' => $file->fid,
- 'module' => 'testing',
- 'type' => 'bar',
- 'id' => 2,
- 'count' => 2
- ))
- ->execute();
- $usage = file_usage_list($file);
- $this->assertEqual(count($usage['testing']), 2, 'Returned the correct number of items.');
- $this->assertTrue(isset($usage['testing']['foo'][1]), 'Returned the correct id.');
- $this->assertTrue(isset($usage['testing']['bar'][2]), 'Returned the correct id.');
- $this->assertEqual($usage['testing']['foo'][1], 1, 'Returned the correct count.');
- $this->assertEqual($usage['testing']['bar'][2], 2, 'Returned the correct count.');
- }
-
- function testAddUsage() {
- $file = $this->createFile();
- file_usage_add($file, 'testing', 'foo', 1);
-
-
- file_usage_add($file, 'testing', 'bar', 2);
- file_usage_add($file, 'testing', 'bar', 2);
- $usage = db_select('file_usage', 'f')
- ->fields('f')
- ->condition('f.fid', $file->fid)
- ->execute()
- ->fetchAllAssoc('id');
- $this->assertEqual(count($usage), 2, 'Created two records');
- $this->assertEqual($usage[1]->module, 'testing', 'Correct module');
- $this->assertEqual($usage[2]->module, 'testing', 'Correct module');
- $this->assertEqual($usage[1]->type, 'foo', 'Correct type');
- $this->assertEqual($usage[2]->type, 'bar', 'Correct type');
- $this->assertEqual($usage[1]->count, 1, 'Correct count');
- $this->assertEqual($usage[2]->count, 2, 'Correct count');
- }
-
- function testRemoveUsage() {
- $file = $this->createFile();
- db_insert('file_usage')
- ->fields(array(
- 'fid' => $file->fid,
- 'module' => 'testing',
- 'type' => 'bar',
- 'id' => 2,
- 'count' => 3,
- ))
- ->execute();
-
- file_usage_delete($file, 'testing', 'bar', 2);
- $count = db_select('file_usage', 'f')
- ->fields('f', array('count'))
- ->condition('f.fid', $file->fid)
- ->execute()
- ->fetchField();
- $this->assertEqual(2, $count, 'The count was decremented correctly.');
-
- file_usage_delete($file, 'testing', 'bar', 2, 2);
- $count = db_select('file_usage', 'f')
- ->fields('f', array('count'))
- ->condition('f.fid', $file->fid)
- ->execute()
- ->fetchField();
- $this->assertIdentical(FALSE, $count, 'The count was removed entirely when empty.');
-
- file_usage_delete($file, 'testing', 'bar', 2);
- $count = db_select('file_usage', 'f')
- ->fields('f', array('count'))
- ->condition('f.fid', $file->fid)
- ->execute()
- ->fetchField();
- $this->assertIdentical(FALSE, $count, 'Decrementing non-exist record complete.');
- }
- }
- class FileValidateTest extends FileHookTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File validate',
- 'description' => 'Tests the file_validate() function.',
- 'group' => 'File API',
- );
- }
-
- function testCallerValidation() {
- $file = $this->createFile();
-
- $this->assertEqual(file_validate($file, array()), array(), 'Validating an empty array works successfully.');
- $this->assertFileHooksCalled(array('validate'));
-
-
- file_test_reset();
- file_test_set_return('validate', array());
- $passing = array('file_test_validator' => array(array()));
- $this->assertEqual(file_validate($file, $passing), array(), 'Validating passes.');
- $this->assertFileHooksCalled(array('validate'));
-
- file_test_reset();
- file_test_set_return('validate', array('Epic fail'));
- $failing = array('file_test_validator' => array(array('Failed', 'Badly')));
- $this->assertEqual(file_validate($file, $failing), array('Failed', 'Badly', 'Epic fail'), 'Validating returns errors.');
- $this->assertFileHooksCalled(array('validate'));
- }
- }
- class FileSaveDataTest extends FileHookTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File save data',
- 'description' => 'Tests the file save data function.',
- 'group' => 'File API',
- );
- }
-
- function testWithoutFilename() {
- $contents = $this->randomName(8);
- $result = file_save_data($contents);
- $this->assertTrue($result, 'Unnamed file saved correctly.');
- $this->assertEqual(file_default_scheme(), file_uri_scheme($result->uri), "File was placed in Drupal's files directory.");
- $this->assertEqual($result->filename, drupal_basename($result->uri), "Filename was set to the file's basename.");
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of the file are correct.');
- $this->assertEqual($result->filemime, 'application/octet-stream', 'A MIME type was set.');
- $this->assertEqual($result->status, FILE_STATUS_PERMANENT, "The file's status was set to permanent.");
-
- $this->assertFileHooksCalled(array('insert'));
-
- $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
- }
-
- function testWithFilename() {
- $contents = $this->randomName(8);
-
- $filename = 'Текстовый файл.txt';
- $result = file_save_data($contents, 'public://' . $filename);
- $this->assertTrue($result, 'Unnamed file saved correctly.');
- $this->assertEqual('public', file_uri_scheme($result->uri), "File was placed in Drupal's files directory.");
- $this->assertEqual($filename, drupal_basename($result->uri), 'File was named correctly.');
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of the file are correct.');
- $this->assertEqual($result->filemime, 'text/plain', 'A MIME type was set.');
- $this->assertEqual($result->status, FILE_STATUS_PERMANENT, "The file's status was set to permanent.");
-
- $this->assertFileHooksCalled(array('insert'));
-
- $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
- }
-
- function testExistingRename() {
-
- $existing = $this->createFile();
- $contents = $this->randomName(8);
- $result = file_save_data($contents, $existing->uri, FILE_EXISTS_RENAME);
- $this->assertTrue($result, 'File saved successfully.');
- $this->assertEqual('public', file_uri_scheme($result->uri), "File was placed in Drupal's files directory.");
- $this->assertEqual($result->filename, $existing->filename, 'Filename was set to the basename of the source, rather than that of the renamed file.');
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of the file are correct.');
- $this->assertEqual($result->filemime, 'application/octet-stream', 'A MIME type was set.');
- $this->assertEqual($result->status, FILE_STATUS_PERMANENT, "The file's status was set to permanent.");
-
- $this->assertFileHooksCalled(array('insert'));
-
- $this->assertDifferentFile($existing, $result);
- $this->assertFileUnchanged($existing, file_load($existing->fid, TRUE));
-
- $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
- }
-
- function testExistingReplace() {
-
- $existing = $this->createFile();
- $contents = $this->randomName(8);
- $result = file_save_data($contents, $existing->uri, FILE_EXISTS_REPLACE);
- $this->assertTrue($result, 'File saved successfully.');
- $this->assertEqual('public', file_uri_scheme($result->uri), "File was placed in Drupal's files directory.");
- $this->assertEqual($result->filename, $existing->filename, 'Filename was set to the basename of the existing file, rather than preserving the original name.');
- $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of the file are correct.');
- $this->assertEqual($result->filemime, 'application/octet-stream', 'A MIME type was set.');
- $this->assertEqual($result->status, FILE_STATUS_PERMANENT, "The file's status was set to permanent.");
-
- $this->assertFileHooksCalled(array('load', 'update'));
-
- $this->assertSameFile($existing, $result);
-
- $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
- }
-
- function testExistingError() {
- $contents = $this->randomName(8);
- $existing = $this->createFile(NULL, $contents);
-
- $result = file_save_data('asdf', $existing->uri, FILE_EXISTS_ERROR);
- $this->assertFalse($result, 'Overwriting a file fails when FILE_EXISTS_ERROR is specified.');
- $this->assertEqual($contents, file_get_contents($existing->uri), 'Contents of existing file were unchanged.');
-
- $this->assertFileHooksCalled(array());
-
- $this->assertFileUnchanged($existing, file_load($existing->fid, TRUE));
- }
- }
- class FileDownloadTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File download',
- 'description' => 'Tests for file download/transfer functions.',
- 'group' => 'File API',
- );
- }
- function setUp() {
- parent::setUp('file_test');
-
- file_test_reset();
- }
-
- function testPublicFileTransfer() {
-
- $file = $this->createFile();
- $url = file_create_url($file->uri);
-
-
- $filename = $GLOBALS['base_url'] . '/' . file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath() . '/' . rawurlencode($file->filename);
- $this->assertEqual($filename, $url, 'Correctly generated a URL for a created file.');
- $this->drupalHead($url);
- $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the created file.');
-
-
- $filepath = 'misc/jquery.js';
- $url = file_create_url($filepath);
- $this->assertEqual($GLOBALS['base_url'] . '/' . $filepath, $url, 'Correctly generated a URL for a shipped file.');
- $this->drupalHead($url);
- $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
- }
-
- function testPrivateFileTransfer() {
-
-
- $contents = $this->randomName(8);
- $file = $this->createFile(NULL, $contents, 'private');
- $url = file_create_url($file->uri);
-
- file_test_set_return('download', array('x-foo' => 'Bar'));
- $this->drupalGet($url);
- $headers = $this->drupalGetHeaders();
- $this->assertEqual($headers['x-foo'], 'Bar', 'Found header set by file_test module on private download.');
- $this->assertResponse(200, 'Correctly allowed access to a file when file_test provides headers.');
-
- $this->assertEqual($contents, $this->content, 'Contents of the file are correct.');
-
- file_test_set_return('download', -1);
- $this->drupalHead($url);
- $this->assertResponse(403, 'Correctly denied access to a file when file_test sets the header to -1.');
-
- $url = file_create_url('private://' . $this->randomName());
- $this->drupalHead($url);
- $this->assertResponse(404, 'Correctly returned 404 response for a non-existent file.');
- }
-
- function testFileCreateUrl() {
- global $base_url;
-
-
-
- $basename = " -._!$'\"()*@[]?&+%#,;=:\n\x00" .
- "%23%25%26%2B%2F%3F" .
- "éøïвβ中國書۞";
- $basename_encoded = '%20-._%21%24%27%22%28%29%2A%40%5B%5D%3F%26%2B%25%23%2C%3B%3D%3A__' .
- '%2523%2525%2526%252B%252F%253F' .
- '%C3%A9%C3%B8%C3%AF%D0%B2%CE%B2%E4%B8%AD%E5%9C%8B%E6%9B%B8%DB%9E';
- $this->checkUrl('public', '', $basename, $base_url . '/' . file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath() . '/' . $basename_encoded);
- $this->checkUrl('private', '', $basename, $base_url . '/system/files/' . $basename_encoded);
- $this->checkUrl('private', '', $basename, $base_url . '/?q=system/files/' . $basename_encoded, '0');
- }
-
- private function checkUrl($scheme, $directory, $filename, $expected_url, $clean_url = '1') {
- variable_set('clean_url', $clean_url);
-
-
-
- $filepath = file_create_filename($filename, $directory);
- $directory_uri = $scheme . '://' . dirname($filepath);
- file_prepare_directory($directory_uri, FILE_CREATE_DIRECTORY);
- $file = $this->createFile($filepath, NULL, $scheme);
- $url = file_create_url($file->uri);
- $this->assertEqual($url, $expected_url, 'Generated URL matches expected URL.');
- if ($scheme == 'private') {
-
-
- file_test_set_return('download', array('x-foo' => 'Bar'));
- }
- $this->drupalGet($url);
- if ($this->assertResponse(200) == 'pass') {
- $this->assertRaw(file_get_contents($file->uri), 'Contents of the file are correct.');
- }
- file_delete($file);
- }
- }
- class FileURLRewritingTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File URL rewriting',
- 'description' => 'Tests for file URL rewriting.',
- 'group' => 'File',
- );
- }
- function setUp() {
- parent::setUp('file_test');
- }
-
- function testShippedFileURL() {
-
-
-
- variable_set('file_test_hook_file_url_alter', 'cdn');
- $filepath = 'misc/jquery.js';
- $url = file_create_url($filepath);
- $this->assertEqual(FILE_URL_TEST_CDN_1 . '/' . $filepath, $url, 'Correctly generated a CDN URL for a shipped file.');
- $filepath = 'misc/favicon.ico';
- $url = file_create_url($filepath);
- $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . $filepath, $url, 'Correctly generated a CDN URL for a shipped file.');
-
- variable_set('file_test_hook_file_url_alter', 'root-relative');
- $filepath = 'misc/jquery.js';
- $url = file_create_url($filepath);
- $this->assertEqual(base_path() . '/' . $filepath, $url, 'Correctly generated a root-relative URL for a shipped file.');
- $filepath = 'misc/favicon.ico';
- $url = file_create_url($filepath);
- $this->assertEqual(base_path() . '/' . $filepath, $url, 'Correctly generated a root-relative URL for a shipped file.');
-
- variable_set('file_test_hook_file_url_alter', 'protocol-relative');
- $filepath = 'misc/jquery.js';
- $url = file_create_url($filepath);
- $this->assertEqual('/' . base_path() . '/' . $filepath, $url, 'Correctly generated a protocol-relative URL for a shipped file.');
- $filepath = 'misc/favicon.ico';
- $url = file_create_url($filepath);
- $this->assertEqual('/' . base_path() . '/' . $filepath, $url, 'Correctly generated a protocol-relative URL for a shipped file.');
- }
-
- function testPublicCreatedFileURL() {
-
-
- variable_set('file_test_hook_file_url_alter', 'cdn');
- $file = $this->createFile();
- $url = file_create_url($file->uri);
- $public_directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
- $this->assertEqual(FILE_URL_TEST_CDN_2 . '/' . $public_directory_path . '/' . $file->filename, $url, 'Correctly generated a CDN URL for a created file.');
-
- variable_set('file_test_hook_file_url_alter', 'root-relative');
- $file = $this->createFile();
- $url = file_create_url($file->uri);
- $this->assertEqual(base_path() . '/' . $public_directory_path . '/' . $file->filename, $url, 'Correctly generated a root-relative URL for a created file.');
-
- variable_set('file_test_hook_file_url_alter', 'protocol-relative');
- $file = $this->createFile();
- $url = file_create_url($file->uri);
- $this->assertEqual('/' . base_path() . '/' . $public_directory_path . '/' . $file->filename, $url, 'Correctly generated a protocol-relative URL for a created file.');
- }
- }
- class FileNameMungingTest extends FileTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File naming',
- 'description' => 'Test filename munging and unmunging.',
- 'group' => 'File API',
- );
- }
- function setUp() {
- parent::setUp();
- $this->bad_extension = 'php';
- $this->name = $this->randomName() . '.' . $this->bad_extension . '.txt';
- $this->name_with_uc_ext = $this->randomName() . '.' . strtoupper($this->bad_extension) . '.txt';
- }
-
- function testMunging() {
-
- variable_set('allow_insecure_uploads', 0);
- $munged_name = file_munge_filename($this->name, '', TRUE);
- $messages = drupal_get_messages();
- $this->assertTrue(in_array(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $munged_name)), $messages['status']), 'Alert properly set when a file is renamed.');
- $this->assertNotEqual($munged_name, $this->name, format_string('The new filename (%munged) has been modified from the original (%original)', array('%munged' => $munged_name, '%original' => $this->name)));
- }
-
- function testMungeNullByte() {
- $prefix = $this->randomName();
- $filename = $prefix . '.' . $this->bad_extension . "\0.txt";
- $this->assertEqual(file_munge_filename($filename, ''), $prefix . '.' . $this->bad_extension . '_.txt', 'A filename with a null byte is correctly munged to remove the null byte.');
- }
-
- function testMungeIgnoreInsecure() {
- variable_set('allow_insecure_uploads', 1);
- $munged_name = file_munge_filename($this->name, '');
- $this->assertIdentical($munged_name, $this->name, format_string('The original filename (%original) matches the munged filename (%munged) when insecure uploads are enabled.', array('%munged' => $munged_name, '%original' => $this->name)));
- }
-
- function testMungeIgnoreWhitelisted() {
-
-
- $munged_name = file_munge_filename($this->name_with_uc_ext, $this->bad_extension);
- $this->assertIdentical($munged_name, $this->name_with_uc_ext, format_string('The new filename (%munged) matches the original (%original) once the extension has been whitelisted.', array('%munged' => $munged_name, '%original' => $this->name_with_uc_ext)));
-
- $munged_name = file_munge_filename($this->name, strtoupper($this->bad_extension));
- $this->assertIdentical($munged_name, $this->name, format_string('The new filename (%munged) matches the original (%original) also when the whitelisted extension is in uppercase.', array('%munged' => $munged_name, '%original' => $this->name)));
- }
-
- function testUnMunge() {
- $munged_name = file_munge_filename($this->name, '', FALSE);
- $unmunged_name = file_unmunge_filename($munged_name);
- $this->assertIdentical($unmunged_name, $this->name, format_string('The unmunged (%unmunged) filename matches the original (%original)', array('%unmunged' => $unmunged_name, '%original' => $this->name)));
- }
- }
- class FileMimeTypeTest extends DrupalWebTestCase {
- function setUp() {
- parent::setUp('file_test');
- }
- public static function getInfo() {
- return array(
- 'name' => 'File mimetypes',
- 'description' => 'Test filename mimetype detection.',
- 'group' => 'File API',
- );
- }
-
- public function testFileMimeTypeDetection() {
- $prefix = 'public://';
- $test_case = array(
- 'test.jar' => 'application/java-archive',
- 'test.jpeg' => 'image/jpeg',
- 'test.JPEG' => 'image/jpeg',
- 'test.jpg' => 'image/jpeg',
- 'test.jar.jpg' => 'image/jpeg',
- 'test.jpg.jar' => 'application/java-archive',
- 'test.pcf.Z' => 'application/x-font',
- 'pcf.z' => 'application/octet-stream',
- 'jar' => 'application/octet-stream',
- 'some.junk' => 'application/octet-stream',
- 'foo.file_test_1' => 'madeup/file_test_1',
- 'foo.file_test_2' => 'madeup/file_test_2',
- 'foo.doc' => 'madeup/doc',
- 'test.ogg' => 'audio/ogg',
- );
-
- foreach ($test_case as $input => $expected) {
-
- $output = file_get_mimetype($prefix . $input);
- $this->assertIdentical($output, $expected, format_string('Mimetype for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
-
- $output = file_get_mimetype($input);
- $this->assertIdentical($output, $expected, format_string('Mimetype (using default mappings) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
- }
-
- $mapping = array(
- 'mimetypes' => array(
- 0 => 'application/java-archive',
- 1 => 'image/jpeg',
- ),
- 'extensions' => array(
- 'jar' => 0,
- 'jpg' => 1,
- )
- );
- $test_case = array(
- 'test.jar' => 'application/java-archive',
- 'test.jpeg' => 'application/octet-stream',
- 'test.jpg' => 'image/jpeg',
- 'test.jar.jpg' => 'image/jpeg',
- 'test.jpg.jar' => 'application/java-archive',
- 'test.pcf.z' => 'application/octet-stream',
- 'pcf.z' => 'application/octet-stream',
- 'jar' => 'application/octet-stream',
- 'some.junk' => 'application/octet-stream',
- 'foo.file_test_1' => 'application/octet-stream',
- 'foo.file_test_2' => 'application/octet-stream',
- 'foo.doc' => 'application/octet-stream',
- 'test.ogg' => 'application/octet-stream',
- );
- foreach ($test_case as $input => $expected) {
- $output = file_get_mimetype($input, $mapping);
- $this->assertIdentical($output, $expected, format_string('Mimetype (using passed-in mappings) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
- }
- }
- }
- class StreamWrapperTest extends DrupalWebTestCase {
- protected $scheme = 'dummy';
- protected $classname = 'DrupalDummyStreamWrapper';
- public static function getInfo() {
- return array(
- 'name' => 'Stream wrappers',
- 'description' => 'Tests stream wrapper functions.',
- 'group' => 'File API',
- );
- }
- function setUp() {
- parent::setUp('file_test');
- drupal_static_reset('file_get_stream_wrappers');
- }
- function tearDown() {
- parent::tearDown();
- stream_wrapper_unregister($this->scheme);
- }
-
- function testGetClassName() {
-
- $this->assertEqual($this->classname, file_stream_wrapper_get_class($this->scheme), 'Got correct class name for dummy scheme.');
-
- $this->assertEqual('DrupalPublicStreamWrapper', file_stream_wrapper_get_class('public'), 'Got correct class name for public scheme.');
- }
-
- function testGetInstanceByScheme() {
- $instance = file_stream_wrapper_get_instance_by_scheme($this->scheme);
- $this->assertEqual($this->classname, get_class($instance), 'Got correct class type for dummy scheme.');
- $instance = file_stream_wrapper_get_instance_by_scheme('public');
- $this->assertEqual('DrupalPublicStreamWrapper', get_class($instance), 'Got correct class type for public scheme.');
- }
-
- function testUriFunctions() {
- $instance = file_stream_wrapper_get_instance_by_uri($this->scheme . '://foo');
- $this->assertEqual($this->classname, get_class($instance), 'Got correct class type for dummy URI.');
- $instance = file_stream_wrapper_get_instance_by_uri('public://foo');
- $this->assertEqual('DrupalPublicStreamWrapper', get_class($instance), 'Got correct class type for public URI.');
-
- $this->assertEqual(file_uri_target('public://foo/bar.txt'), 'foo/bar.txt', 'Got a valid stream target from public://foo/bar.txt.');
- $this->assertFalse(file_uri_target('foo/bar.txt'), 'foo/bar.txt is not a valid stream.');
-
- $this->assertEqual(file_build_uri('foo/bar.txt'), 'public://foo/bar.txt', 'Expected scheme was added.');
- $this->assertEqual(file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath(), variable_get('file_public_path'), 'Expected default directory path was returned.');
- $this->assertEqual(file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath(), variable_get('file_temporary_path'), 'Expected temporary directory path was returned.');
- variable_set('file_default_scheme', 'private');
- $this->assertEqual(file_build_uri('foo/bar.txt'), 'private://foo/bar.txt', 'Got a valid URI from foo/bar.txt.');
- }
-
- function testGetValidStreamScheme() {
- $this->assertEqual('foo', file_uri_scheme('foo://pork//chops'), 'Got the correct scheme from foo://asdf');
- $this->assertTrue(file_stream_wrapper_valid_scheme(file_uri_scheme('public://asdf')), 'Got a valid stream scheme from public://asdf');
- $this->assertFalse(file_stream_wrapper_valid_scheme(file_uri_scheme('foo://asdf')), 'Did not get a valid stream scheme from foo://asdf');
- }
-
- public function testPharStreamWrapperRegistration() {
- if (!class_exists('Phar', FALSE)) {
- $this->assertFalse(in_array('phar', stream_get_wrappers(), TRUE), 'PHP is compiled without phar support. Therefore, no phar stream wrapper is registered.');
- }
- elseif (version_compare(PHP_VERSION, '5.3.3', '<')) {
- $this->assertFalse(in_array('phar', stream_get_wrappers(), TRUE), 'The PHP version is <5.3.3. The built-in phar stream wrapper has been unregistered and not replaced.');
- }
- else {
- $this->assertTrue(in_array('phar', stream_get_wrappers(), TRUE), 'A phar stream wrapper is registered.');
- $this->assertFalse(file_stream_wrapper_valid_scheme('phar'), 'The phar scheme is not a valid scheme for Drupal File API usage.');
- }
-
-
-
- file_get_stream_wrappers();
- file_get_stream_wrappers();
- drupal_static_reset('file_get_stream_wrappers');
- file_get_stream_wrappers();
- }
-
- public function testPharFile() {
- if (!in_array('phar', stream_get_wrappers(), TRUE)) {
- $this->pass('There is no phar stream wrapper registered.');
-
-
-
-
- return;
- }
- $base = dirname(dirname(__FILE__)) . '/files';
-
-
- $this->assertFalse(file_exists("phar://$base/phar-1.phar/no-such-file.php"));
- $this->assertTrue(file_exists("phar://$base/phar-1.phar/index.php"));
- $file_contents = file_get_contents("phar://$base/phar-1.phar/index.php");
- $expected_hash = 'c7e7904ea573c5ebea3ef00bb08c1f86af1a45961fbfbeb1892ff4a98fd73ad5';
- $this->assertIdentical($expected_hash, hash('sha256', $file_contents));
-
-
- try {
- file_exists("phar://$base/image-2.jpg/index.php");
- $this->fail('Expected exception failed to be thrown when accessing an invalid phar file.');
- }
- catch (Exception $e) {
- $this->assertEqual(get_class($e), 'TYPO3\PharStreamWrapper\Exception', 'Expected exception thrown when accessing an invalid phar file.');
- }
- }
- }
|