function SaveUploadFormTest::setUp

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::setUp()
  2. 10 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::setUp()
  3. 11.x core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::setUp()

Overrides FileManagedTestBase::setUp

File

core/modules/file/tests/src/Functional/SaveUploadFormTest.php, line 65

Class

SaveUploadFormTest
Tests the _file_save_upload_from_form() function.

Namespace

Drupal\Tests\file\Functional

Code

protected function setUp() {
    parent::setUp();
    $account = $this->drupalCreateUser([
        'access site reports',
    ]);
    $this->drupalLogin($account);
    $image_files = $this->drupalGetTestFiles('image');
    $this->image = File::create((array) current($image_files));
    list(, $this->imageExtension) = explode('.', $this->image
        ->getFilename());
    $this->assertFileExists($this->image
        ->getFileUri());
    $this->phpfile = current($this->drupalGetTestFiles('php'));
    $this->assertFileExists($this->phpfile->uri);
    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')
        ->execute()[0]['fid_max'];
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    // Upload with replace to guarantee there's something there.
    $edit = [
        'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
        'files[file_test_upload][]' => $file_system->realpath($this->image
            ->getFileUri()),
    ];
    $this->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertRaw(t('You WIN!'), 'Found the success message.');
    // Check that the correct hooks were called then clean out the hook
    // counters.
    $this->assertFileHooksCalled([
        'validate',
        'insert',
    ]);
    file_test_reset();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.