/** * Test that the validators passed into are checked. */ function testCallerValidation() { $file = $this->createFile(); // Empty validators. $this->assertEqual(file_validate($file, array()), array(), 'Validating an empty array works successfully.'); $this->assertFileHooksCalled(array('validate')); // Use the file_test.module's test validator to ensure that passing tests // return correctly. 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')); // Now test for failures in validators passed in and by hook_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')); }
/** * Download a file from the URL generated by file_create_url(). * * Create a file with the specified scheme, directory and filename; check that * the URL generated by file_create_url() for the specified file equals the * specified URL; fetch the URL and then compare the contents to the file. * * @param string $scheme * A scheme, e.g. "public". * @param string $directory * A directory, possibly "". * @param string $filename * A filename. * @param string $expected_url * The expected URL. */ private function checkUrl($scheme, $directory, $filename, $expected_url) { // Convert $filename to a valid filename, i.e. strip characters not // supported by the filesystem, and create the file in the specified // directory. $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->getFileUri()); $this->assertEqual($url, $expected_url); if ($scheme == 'private') { // Tell the implementation of hook_file_download() in file_test.module // that this file may be downloaded. file_test_set_return('download', array('x-foo' => 'Bar')); } $this->drupalGet($url); if ($this->assertResponse(200) == 'pass') { $this->assertRaw(file_get_contents($file->getFileUri()), 'Contents of the file are correct.'); } $file->delete(); }