View source
<?php
namespace Drupal\Tests\system\FunctionalJavascript\Form;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class RebuildTest extends WebDriverTestBase {
protected static $modules = [
'node',
'form_test',
];
protected $defaultTheme = 'stark';
protected $webUser;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->webUser = $this
->drupalCreateUser([
'access content',
]);
$this
->drupalLogin($this->webUser);
}
public function testPreserveFormActionAfterAJAX() {
$page = $this
->getSession()
->getPage();
$field_name = 'field_ajax_test';
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'text',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => 'page',
])
->save();
$field_file_name = 'field_file_test';
FieldStorageConfig::create([
'field_name' => $field_file_name,
'entity_type' => 'node',
'type' => 'file',
'cardinality' => 1,
])
->save();
FieldConfig::create([
'field_name' => $field_file_name,
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Test file',
'required' => TRUE,
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'page', 'default')
->setComponent($field_name, [
'type' => 'text_textfield',
])
->setComponent($field_file_name, [
'type' => 'file_generic',
])
->save();
$this->webUser = $this
->drupalCreateUser([
'create page content',
]);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/add/page');
$page
->find('css', '[value="Add another item"]')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->elementsCount('xpath', '//div[contains(@class, "field--name-field-ajax-test")]//input[@type="text"]', 2);
$edit = [
'title[0][value]' => $this
->randomString(),
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Test file field is required.');
$this
->assertSession()
->elementsCount('xpath', '//form[contains(@id, "node-page-form")]//div[contains(@class, "js-form-item-field-ajax-test")]//input[@type="text"]', 2);
$forms = $this
->xpath('//form[contains(@class, "node-page-form")]');
$this
->assertCount(1, $forms);
$url = parse_url($forms[0]
->getAttribute('action'))['path'];
$this
->assertEquals(Url::fromRoute('node.add', [
'node_type' => 'page',
])
->toString(), $url);
}
}