file_test_reset example


  protected static $modules = ['file_test', 'file'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Clear out any hook calls.     file_test_reset();
  }

  /** * Asserts that the specified file hooks were called only once. * * @param string[] $expected * An array of strings containing with the hook name; for example, 'load', * 'save', 'insert', etc. */
  public function assertFileHooksCalled($expected) {
    \Drupal::state()->resetCache();

    

  protected static $modules = ['file_test', 'file', 'system', 'field', 'user'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Clear out any hook calls.     file_test_reset();

    $this->installConfig(['system']);
    $this->installEntitySchema('file');
    $this->installEntitySchema('user');
    $this->installSchema('file', ['file_usage']);

    // Make sure that a user with uid 1 exists, self::createFile() relies on     // it.     $user = User::create(['uid' => 1, 'name' => $this->randomMachineName()]);
    $user->enforceIsNew();
    $user->save();
    
$this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
  }

  /** * This will test loading file data from the database. */
  public function testMultiple() {
    // Create a new file entity.     $file = $this->createFile('druplicon.txt', NULL, 'public');

    // Load by path.     file_test_reset();
    $by_path_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $file->getFileUri()]);
    $this->assertFileHookCalled('load');
    $this->assertCount(1, $by_path_files, '\Drupal::entityTypeManager()->getStorage(\'file\')->loadByProperties() 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->assertEquals($file->id()$by_path_file->id(), 'Loading by filepath got the correct fid.');

    // Load by fid.     file_test_reset();
    $by_fid_files = File::loadMultiple([$file->id()]);
    $this->assertFileHooksCalled([]);
    
$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->assertEquals([1 => 1]$usage['testing']['test'], 'Test file is still in use.');
    $this->assertFileExists($file->getFileUri());
    $this->assertNotEmpty(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(['load', 'update']);
    $this->assertEmpty($usage, 'File usage data was removed.');
    $this->assertFileExists($file->getFileUri());
    $file = File::load($file->id());
    $this->assertNotEmpty($file, 'File still exists in the database.');
    $this->assertTrue($file->isTemporary(), 'File is temporary.');
    file_test_reset();

    

  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();
    file_test_set_return('validate', ['Epic fail']);
    $failing = ['file_test_validator' => [['Failed', 'Badly']]];
    $this->assertEquals(['Failed', 'Badly', 'Epic fail']file_validate($file$failing), 'Validating returns errors.');
    $this->assertFileHooksCalled(['validate']);
  }
// This test currently frequently causes the SQLite database to lock, so     // skip the test on SQLite until the issue can be resolved.     // @todo Fix root cause and re-enable in     // https://www.drupal.org/project/drupal/issues/3311587     if (Database::getConnection()->driver() === 'sqlite') {
      $this->markTestSkipped('Test frequently causes a locked database on SQLite');
    }

    $this->fileUrlGenerator = $this->container->get('file_url_generator');
    // Clear out any hook calls.     file_test_reset();
  }

  /** * Tests the public file transfer system. */
  public function testPublicFileTransfer() {
    // Test generating a URL to a created file.     $file = $this->createFile();
    $url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());
    // URLs can't contain characters outside the ASCII set so $filename has to be     // encoded.
// 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.');

    // 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(['load', 'update']);

    $this->assertEquals($file->id()$file->id(), 'The file ID of an existing file is not changed when updating the database.');
    $loaded_file = File::load($file->id());
    // Verify that the timestamp didn't go backwards.     $this->assertGreaterThanOrEqual($file->getChangedTime()$loaded_file->getChangedTime());
    $this->assertNotNull($loaded_file, 'Record still exists in the database.');
    
'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'];
    // Verify that a new file was created.
'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'];
    // Verify that a new file was created.
Home | Imprint | This part of the site doesn't use cookies.