assertFileHooksCalled example

'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
      'files[file_test_upload]' => \Drupal::service('file_system')->realpath($this->image->getFileUri()),
    ];
    $this->drupalGet('file-test/upload');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()->statusCodeEquals(200);
    // Check that the success message is present.     $this->assertSession()->pageTextContains("You WIN!");

    // Check that the correct hooks were called then clean out the hook     // counters.     $this->assertFileHooksCalled(['validate', 'insert']);
    file_test_reset();
  }

  /** * Tests the file_save_upload() function. */
  public function testNormal() {
    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];
    
class DeleteTest extends FileManagedUnitTestBase {

  /** * Tries deleting a normal file (as opposed to a directory, symlink, etc). */
  public function testUnused() {
    $file = $this->createFile();

    // Check that deletion removes the file and database record.     $this->assertFileExists($file->getFileUri());
    $file->delete();
    $this->assertFileHooksCalled(['delete']);
    $this->assertFileDoesNotExist($file->getFileUri());
    $this->assertNull(File::load($file->id()), 'File was removed from the database.');
  }

  /** * Tries deleting a file that is in use. */
  public function testInUse() {
    // This test expects unused managed files to be marked as a temporary file     // and then deleted up by file_cron().     $this->config('file.settings')
      
$desired_uri = 'public://' . $this->randomMachineName();

    // Clone the object so we don't have to worry about the function changing     // our reference copy.     $result = $this->fileRepository->copy(clone $source$desired_uri, FileSystemInterface::EXISTS_ERROR);

    // Check the return status and that the contents changed.     $this->assertNotFalse($result, 'File copied successfully.');
    $this->assertEquals($contentsfile_get_contents($result->getFileUri()), 'Contents of file were copied correctly.');

    // Check that the correct hooks were called.     $this->assertFileHooksCalled(['copy', 'insert']);

    $this->assertDifferentFile($source$result);
    $this->assertEquals($result->getFileUri()$desired_uri, 'The copied file entity has the desired filepath.');
    $this->assertFileExists($source->getFileUri());
    $this->assertFileExists($result->getFileUri());

    // Reload the file from the database and check that the changes were     // actually saved.     $this->assertFileUnchanged($result, File::load($result->id()));
  }

  

class ValidateTest extends FileManagedUnitTestBase {

  /** * Tests that the validators passed into are checked. */
  public function testCallerValidation() {
    $file = $this->createFile();

    // Empty validators.     $this->assertEquals([]file_validate($file[]), 'Validating an empty array works successfully.');
    $this->assertFileHooksCalled(['validate']);

    // Use the file_test.module's test validator to ensure that passing tests     // return correctly.     file_test_reset();
    file_test_set_return('validate', []);
    $passing = ['file_test_validator' => [[]]];
    $this->assertEquals([]file_validate($file$passing), 'Validating passes.');
    $this->assertFileHooksCalled(['validate']);

    // Now test for failures in validators passed in and by hook_validate.     file_test_reset();
    
'filename' => 'druplicon.txt',
      'uri' => 'public://druplicon.txt',
      'filemime' => 'text/plain',
    ]);
    $file->setPermanent();
    file_put_contents($file->getFileUri(), 'hello world');

    // Save it, inserting a new record.     $file->save();

    // Check that the correct hooks were called.     $this->assertFileHooksCalled(['insert']);

    // Verify that a new file ID is set when saving a new file to the database.     $this->assertGreaterThan(0, $file->id());
    $loaded_file = File::load($file->id());
    $this->assertNotNull($loaded_file, 'Record exists in the database.');
    $this->assertEquals($file->isPermanent()$loaded_file->isPermanent(), 'Status was saved correctly.');
    $this->assertEquals(filesize($file->getFileUri())$file->getSize(), 'File size was set correctly.');
    // Verify that the new file size was set correctly.     $this->assertGreaterThan(1, $file->getChangedTime());
    $this->assertEquals('en', $loaded_file->langcode->value, 'Langcode was defaulted correctly.');

    
// Clone the object so we don't have to worry about the function changing     // our reference copy.     $result = $this->fileRepository->move(clone $source$desired_filepath, FileSystemInterface::EXISTS_ERROR);

    // Check the return status and that the contents changed.     $this->assertNotFalse($result, 'File moved successfully.');
    $this->assertFileDoesNotExist($source->getFileUri());
    $this->assertEquals($contentsfile_get_contents($result->getFileUri()), 'Contents of file correctly written.');

    // Check that the correct hooks were called.     $this->assertFileHooksCalled(['move', 'load', 'update']);

    // Make sure we got the same file back.     $this->assertEquals($source->id()$result->id()new FormattableMarkup("Source file id's' %fid is unchanged after move.", ['%fid' => $source->id()]));

    // Reload the file from the database and check that the changes were     // actually saved.     $loaded_file = File::load($result->id());
    $this->assertNotEmpty($loaded_file, 'File can be loaded from the database.');
    $this->assertFileUnchanged($result$loaded_file);
  }

  
$this->assertNotFalse($result, 'Unnamed file saved correctly.');

    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
    $this->assertEquals('public', $stream_wrapper_manager::getScheme($result->getFileUri()), "File was placed in Drupal's files directory.");
    $this->assertEquals($filename, \Drupal::service('file_system')->basename($result->getFileUri()), 'File was named correctly.');
    $this->assertEquals($contentsfile_get_contents($result->getFileUri()), 'Contents of the file are correct.');
    $this->assertEquals('text/plain', $result->getMimeType(), 'A MIME type was set.');
    $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");

    // Check that the correct hooks were called.     $this->assertFileHooksCalled(['insert']);

    // Verify that what was returned is what's in the database.     $this->assertFileUnchanged($result, File::load($result->id()));
  }

  /** * Tests writeData() when renaming around an existing file. * * @covers ::writeData */
  public function testExistingRename() {
    

class LoadTest extends FileManagedUnitTestBase {

  /** * Try to load a non-existent file by fid. */
  public function testLoadMissingFid() {
    $this->assertNull(File::load(-1), 'Try to load an invalid fid fails.');
    $this->assertFileHooksCalled([]);
  }

  /** * Try to load a non-existent file by URI. */
  public function testLoadMissingFilepath() {
    $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => 'foobar://misc/druplicon.png']);
    $this->assertFalse(reset($files), "Try to load a file that doesn't exist in the database fails.");
    $this->assertFileHooksCalled([]);
  }

  
    $edit = [
      'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
      'files[file_test_upload][]' => $file_system->realpath($this->image->getFileUri()),
    ];
    $this->drupalGet('file-test/save_upload_from_form_test');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains("You WIN!");

    // Check that the correct hooks were called then clean out the hook     // counters.     $this->assertFileHooksCalled(['validate', 'insert']);
    file_test_reset();
  }

  /** * Tests the _file_save_upload_from_form() function. */
  public function testNormal() {
    $max_fid_after = (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];
    
Home | Imprint | This part of the site doesn't use cookies.