function FileFieldAnonymousSubmissionTest::testAnonymousNodeWithFile

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

Tests file submission for an anonymous visitor.

File

core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php, line 67

Class

FileFieldAnonymousSubmissionTest
Confirm that file field submissions work correctly for anonymous visitors.

Namespace

Drupal\Tests\file\Functional

Code

public function testAnonymousNodeWithFile() : void {
    $type = 'Article';
    $title = 'Test page';
    $this->createFileField('field_image', 'node', 'article', [], [
        'file_extensions' => 'txt png',
    ]);
    // Load the node form.
    $this->drupalLogout();
    $this->drupalGet('node/add/article');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains("Create {$type}");
    // Generate an image file.
    $image = $this->getTestFile('image');
    // Submit the form.
    $edit = [
        'title[0][value]' => $title,
        'body[0][value]' => 'Test article',
        'files[field_image_0]' => $this->container
            ->get('file_system')
            ->realpath($image->getFileUri()),
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains("{$type} {$title} has been created.");
    $matches = [];
    if (preg_match('@node/(\\d+)$@', $this->getUrl(), $matches)) {
        $nid = end($matches);
        $this->assertNotEquals(0, $nid, 'The node ID was extracted from the URL.');
        $node = Node::load($nid);
        $this->assertNotNull($node, 'The node was loaded successfully.');
        $this->assertFileExists(File::load($node->field_image->target_id)
            ->getFileUri());
    }
}

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