assertDifferentFile example

/** * Tests renaming when moving onto a file that already exists. * * @covers ::move */
  public function testExistingRename() {
    // Setup a file to overwrite.     $contents = $this->randomMachineName(10);
    $source = $this->createFile(NULL, $contents);
    $target = $this->createFile();
    $this->assertDifferentFile($source$target);

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

    // 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.
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($existing->getFilename()$result->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
    $this->assertEquals($contentsfile_get_contents($result->getFileUri()), 'Contents of the file are correct.');
    $this->assertEquals('application/octet-stream', $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']);

    // Ensure that the existing file wasn't overwritten.     $this->assertDifferentFile($existing$result);
    $this->assertFileUnchanged($existing, File::load($existing->id()));

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

  /** * Tests writeData() when replacing an existing file. * * @covers ::writeData */
  
// 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()));
  }

  /** * Tests renaming when copying over a file that already exists. * * @covers ::copy */
Home | Imprint | This part of the site doesn't use cookies.