ImageFieldValidateTest.php in Drupal 10
File
core/modules/image/tests/src/FunctionalJavascript/ImageFieldValidateTest.php
View source
<?php
namespace Drupal\Tests\image\FunctionalJavascript;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
class ImageFieldValidateTest extends ImageFieldTestBase {
protected $defaultTheme = 'stark';
public function testAJAXValidationMessage() {
$field_name = strtolower($this
->randomMachineName());
$this
->createImageField($field_name, 'article', [
'cardinality' => -1,
]);
$this
->drupalGet('node/add/article');
$text_files = $this
->drupalGetTestFiles('text');
$text_file = reset($text_files);
$field = $this
->getSession()
->getPage()
->findField('files[' . $field_name . '_0][]');
$field
->attachFile($this->container
->get('file_system')
->realpath($text_file->uri));
$this
->assertSession()
->waitForElement('css', '.messages--error');
$this
->assertSession()
->elementsCount('xpath', '//div[contains(@class, "messages--error")]', 1);
}
public function testFriendlyAjaxValidation() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_dummy_select',
'type' => 'image_module_test_dummy_ajax',
'entity_type' => 'node',
'cardinality' => 1,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'node',
'bundle' => 'article',
'field_name' => 'field_dummy_select',
'label' => 'Dummy select',
])
->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.article.default')
->setComponent('field_dummy_select', [
'type' => 'image_module_test_dummy_ajax_widget',
'weight' => 1,
])
->save();
$this
->createImageField('field_dummy_image', 'article');
$this
->drupalGet('node/add/article');
$id = $this
->getSession()
->getPage()
->find('css', '[name="form_build_id"]')
->getValue();
$field = $this
->getSession()
->getPage()
->findField('field_dummy_select[select_widget]');
$field
->setValue('bam');
$this
->assertSession()
->waitForElement('css', "[name='form_build_id']:not([value='{$id}'])");
}
}