Example #1
0
 /**
  * Tries deleting a file that is in use.
  */
 function testInUse()
 {
     $file = $this->createFile();
     $file_usage = $this->container->get('file.usage');
     $file_usage->add($file, 'testing', 'test', 1);
     $file_usage->add($file, 'testing', 'test', 1);
     $file_usage->delete($file, 'testing', 'test', 1);
     $usage = $file_usage->listUsage($file);
     $this->assertEqual($usage['testing']['test'], array(1 => 1), 'Test file is still in use.');
     $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
     $this->assertTrue(File::load($file->id()), 'File still exists in the database.');
     // Clear out the call to hook_file_load().
     file_test_reset();
     $file_usage->delete($file, 'testing', 'test', 1);
     $usage = $file_usage->listUsage($file);
     $this->assertFileHooksCalled(array('load', 'update'));
     $this->assertTrue(empty($usage), 'File usage data was removed.');
     $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
     $file = File::load($file->id());
     $this->assertTrue($file, 'File still exists in the database.');
     $this->assertTrue($file->isTemporary(), 'File is temporary.');
     file_test_reset();
     // Call file_cron() to clean up the file. Make sure the changed timestamp
     // of the file is older than the system.file.temporary_maximum_age
     // configuration value.
     db_update('file_managed')->fields(array('changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1)))->condition('fid', $file->id())->execute();
     \Drupal::service('cron')->run();
     // file_cron() loads
     $this->assertFileHooksCalled(array('delete'));
     $this->assertFalse(file_exists($file->getFileUri()), 'File has been deleted after its last usage was removed.');
     $this->assertFalse(File::load($file->id()), 'File was removed from the database.');
 }
 function testFileSave()
 {
     // Create a new file entity.
     $file = entity_create('file', array('uid' => 1, 'filename' => 'druplicon.txt', 'uri' => 'public://druplicon.txt', 'filemime' => 'text/plain', 'created' => 1, 'changed' => 1, 'status' => FILE_STATUS_PERMANENT));
     file_put_contents($file->getFileUri(), 'hello world');
     // Save it, inserting a new record.
     $file->save();
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('insert'));
     $this->assertTrue($file->id() > 0, 'A new file ID is set when saving a new file to the database.', 'File');
     $loaded_file = file_load($file->id());
     $this->assertNotNull($loaded_file, 'Record exists in the database.');
     $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
     $this->assertEqual($file->getSize(), filesize($file->getFileUri()), 'File size was set correctly.', 'File');
     $this->assertTrue($file->getChangedTime() > 1, 'File size was set correctly.', 'File');
     $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was defaulted correctly.');
     // Resave the file, updating the existing record.
     file_test_reset();
     $file->status->value = 7;
     $file->save();
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('load', 'update'));
     $this->assertEqual($file->id(), $file->id(), 'The file ID of an existing file is not changed when updating the database.', 'File');
     $this->assertTrue($file->getChangedTime() >= $file->getChangedTime(), "Timestamp didn't go backwards.", 'File');
     $loaded_file = file_load($file->id());
     $this->assertNotNull($loaded_file, 'Record still exists in the database.', 'File');
     $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
     $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was saved correctly.');
     // Try to insert a second file with the same name apart from case insensitivity
     // to ensure the 'uri' index allows for filenames with different cases.
     $file = entity_create('file', array('uid' => 1, 'filename' => 'DRUPLICON.txt', 'uri' => 'public://DRUPLICON.txt', 'filemime' => 'text/plain', 'created' => 1, 'changed' => 1, 'status' => FILE_STATUS_PERMANENT));
     file_put_contents($file->getFileUri(), 'hello world');
     $file->save();
 }
 protected function setUp()
 {
     parent::setUp();
     // Clear out any hook calls.
     file_test_reset();
     $this->installConfig(array('system'));
     $this->installEntitySchema('file');
     $this->installEntitySchema('user');
     $this->installSchema('file', array('file_usage'));
     // Make sure that a user with uid 1 exists, self::createFile() relies on
     // it.
     $user = entity_create('user', array('uid' => 1, 'name' => $this->randomMachineName()));
     $user->enforceIsNew();
     $user->save();
     \Drupal::currentUser()->setAccount($user);
 }
 function testFileSave()
 {
     // Create a new file entity.
     $file = File::create(array('uid' => 1, 'filename' => 'druplicon.txt', 'uri' => 'public://druplicon.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT));
     file_put_contents($file->getFileUri(), 'hello world');
     // Save it, inserting a new record.
     $file->save();
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('insert'));
     $this->assertTrue($file->id() > 0, 'A new file ID is set when saving a new file to the database.', 'File');
     $loaded_file = file_load($file->id());
     $this->assertNotNull($loaded_file, 'Record exists in the database.');
     $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
     $this->assertEqual($file->getSize(), filesize($file->getFileUri()), 'File size was set correctly.', 'File');
     $this->assertTrue($file->getChangedTime() > 1, 'File size was set correctly.', 'File');
     $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was defaulted correctly.');
     // Resave the file, updating the existing record.
     file_test_reset();
     $file->status->value = 7;
     $file->save();
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('load', 'update'));
     $this->assertEqual($file->id(), $file->id(), 'The file ID of an existing file is not changed when updating the database.', 'File');
     $this->assertTrue($file->getChangedTime() >= $file->getChangedTime(), "Timestamp didn't go backwards.", 'File');
     $loaded_file = file_load($file->id());
     $this->assertNotNull($loaded_file, 'Record still exists in the database.', 'File');
     $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
     $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was saved correctly.');
     // Try to insert a second file with the same name apart from case insensitivity
     // to ensure the 'uri' index allows for filenames with different cases.
     $uppercase_values = array('uid' => 1, 'filename' => 'DRUPLICON.txt', 'uri' => 'public://DRUPLICON.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT);
     $uppercase_file = File::create($uppercase_values);
     file_put_contents($uppercase_file->getFileUri(), 'hello world');
     $violations = $uppercase_file->validate();
     $this->assertEqual(count($violations), 0, 'No violations when adding an URI with an existing filename in upper case.');
     $uppercase_file->save();
     // Ensure the database URI uniqueness constraint is triggered.
     $uppercase_file_duplicate = File::create($uppercase_values);
     file_put_contents($uppercase_file_duplicate->getFileUri(), 'hello world');
     $violations = $uppercase_file_duplicate->validate();
     $this->assertEqual(count($violations), 1);
     $this->assertEqual($violations[0]->getMessage(), t('The file %value already exists. Enter a unique file URI.', ['%value' => $uppercase_file_duplicate->getFileUri()]));
     // Ensure that file URI entity queries are case sensitive.
     $fids = \Drupal::entityQuery('file')->condition('uri', $uppercase_file->getFileUri())->execute();
     $this->assertEqual(1, count($fids));
     $this->assertEqual(array($uppercase_file->id() => $uppercase_file->id()), $fids);
 }
Example #5
0
 /**
  * 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'));
 }
 protected function setUp()
 {
     parent::setUp();
     // Clear out any hook calls.
     file_test_reset();
 }
Example #7
0
 /**
  * Test file munge handling.
  */
 function testHandleFileMunge()
 {
     // Ensure insecure uploads are disabled for this test.
     $this->config('system.file')->set('allow_insecure_uploads', 0)->save();
     $this->image = file_move($this->image, $this->image->getFileUri() . '.foo.' . $this->imageExtension);
     // Reset the hook counters to get rid of the 'move' we just called.
     file_test_reset();
     $extensions = $this->imageExtension;
     $edit = array('files[file_test_upload]' => drupal_realpath($this->image->getFileUri()), 'extensions' => $extensions);
     $munged_filename = $this->image->getFilename();
     $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
     $munged_filename .= '_.' . $this->imageExtension;
     $this->drupalPostForm('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.');
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('validate', 'insert'));
     // Ensure we don't munge files if we're allowing any extension.
     // Reset the hook counters.
     file_test_reset();
     $edit = array('files[file_test_upload]' => drupal_realpath($this->image->getFileUri()), 'allow_all_extensions' => TRUE);
     $this->drupalPostForm('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->getFilename())), 'File was not munged when allowing any extension.');
     $this->assertRaw(t('You WIN!'), 'Found the success message.');
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('validate', 'insert'));
 }
Example #8
0
 /**
  * Loads a single file and ensure that the correct values are returned.
  */
 public function testUuidValues()
 {
     // Create a new file entity from scratch so we know the values.
     $file = $this->createFile('druplicon.txt', NULL, 'public');
     $file->save();
     file_test_reset();
     $by_uuid_file = \Drupal::entityManager()->loadEntityByUuid('file', $file->uuid());
     $this->assertFileHookCalled('load');
     $this->assertTrue(is_object($by_uuid_file), '\\Drupal::entityManager()->loadEntityByUuid() returned a file object.');
     if (is_object($by_uuid_file)) {
         $this->assertEqual($by_uuid_file->id(), $file->id(), 'Loading by UUID got the same fid.', 'File');
         $this->assertTrue($by_uuid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
     }
 }