View source
<?php
namespace Drupal\Tests\file\Functional;
use Drupal\node\Entity\Node;
use Drupal\user\RoleInterface;
use Drupal\file\Entity\File;
class FileFieldAnonymousSubmissionTest extends FileFieldTestBase {
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'create article content' => TRUE,
'access content' => TRUE,
]);
}
public function testAnonymousNode() {
$type = 'Article';
$title = 'test page';
$this
->drupalLogout();
$this
->drupalGet('node/add/article');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("Create {$type}");
$edit = [
'title[0][value]' => $title,
'body[0][value]' => 'Test article',
];
$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.');
}
}
public function testAnonymousNodeWithFile() {
$type = 'Article';
$title = 'Test page';
$this
->createFileField('field_image', 'node', 'article', [], [
'file_extensions' => 'txt png',
]);
$this
->drupalLogout();
$this
->drupalGet('node/add/article');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("Create {$type}");
$image = $this
->getTestFile('image');
$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());
}
}
public function testAnonymousNodeWithFileWithoutTitle() {
$this
->drupalLogout();
$this
->doTestNodeWithFileWithoutTitle();
}
public function testAuthenticatedNodeWithFileWithoutTitle() {
$admin_user = $this
->drupalCreateUser([
'bypass node access',
'access content overview',
'administer nodes',
]);
$this
->drupalLogin($admin_user);
$this
->doTestNodeWithFileWithoutTitle();
}
protected function doTestNodeWithFileWithoutTitle() {
$type = 'Article';
$title = 'Test page';
$this
->createFileField('field_image', 'node', 'article', [], [
'file_extensions' => 'txt png',
]);
$this
->drupalGet('node/add/article');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("Create {$type}");
$image = $this
->getTestFile('image');
$edit = [
'body[0][value]' => 'Test article',
'files[field_image_0]' => $this->container
->get('file_system')
->realpath($image
->getFileUri()),
];
if (!$this->loggedInUser) {
$label = 'Save';
}
else {
$label = 'Save';
}
$this
->submitForm($edit, $label);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains("{$type} {$title} has been created.");
$this
->assertSession()
->pageTextContains('Title field is required.');
$edit = [
'title[0][value]' => $title,
];
$this
->submitForm($edit, $label);
$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());
}
}
}