View source
<?php
namespace Drupal\Tests\imagefield_tokens\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\image\Functional\ImageFieldTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\file\Entity\File;
class ImageFieldTokensWidgetTest extends ImageFieldTestBase {
protected $strictConfigSchema = FALSE;
public static $modules = [
'node',
'image',
'token',
'imagefield_tokens',
];
use AssertPageCacheContextsAndTagsTrait;
use ImageFieldTokensTestingTrait;
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
compareFiles as drupalCompareFiles;
}
public function testWidgetElement() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$field_name = strtolower($this
->randomMachineName());
$min_resolution = 50;
$max_resolution = 100;
$field_settings = [
'max_resolution' => $max_resolution . 'x' . $max_resolution,
'min_resolution' => $min_resolution . 'x' . $min_resolution,
'alt_field' => 1,
];
$this
->createImageFieldTokensField($field_name, 'article', [
'uri_scheme' => 'public',
], $field_settings);
$this
->drupalGet('node/add/article');
$this
->assertNotEqual(0, count($this
->xpath('//div[contains(@class, "field--widget-imagefield-tokens")]')), 'Image field widget found on add/node page', 'Browser');
$this
->assertNotEqual(0, count($this
->xpath('//input[contains(@accept, "image/*")]')), 'Image field widget limits accepted files.', 'Browser');
$this
->assertSession()
->pageTextNotContains('Image test on [site:name]');
$this
->assertSession()
->pageTextContains('Allowed types: png gif jpg jpeg.');
$field_config = FieldConfig::loadByName('node', 'article', $field_name);
$field_config
->setSetting('file_extensions', 'png gif jpg jpeg tiff')
->save();
$this
->drupalGet('node/add/article');
$this
->assertSession()
->pageTextContains('Allowed types: png gif jpg jpeg.');
$field_config
->setSetting('file_extensions', 'png jpe tiff')
->save();
$this
->drupalGet('node/add/article');
$this
->assertSession()
->pageTextContains('Allowed types: png jpe.');
$test_image = current($this
->drupalGetTestFiles('image'));
$this
->previewNodeImage($test_image, $field_name, 'article');
$alt = '[node:title]';
Node::create([
'title' => $this
->randomMachineName(),
'type' => 'article',
])
->save();
$nid = 1;
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$file = File::Create([
'uri' => $test_image->uri,
]);
$file
->save();
$node->{$field_name}
->setValue([
'target_id' => $file
->id(),
'alt' => $alt,
]);
$node
->save();
$this
->drupalGet('node/1/edit');
$path = "//input[@id='edit-" . $field_name . "-0-alt']";
$xpath = $this
->xpath($path);
self::assertEquals($xpath[0]
->getValue(), $node
->getTitle(), 'Make sure ALT field has been processed correctly!');
}
}