Commit e1ffaa77 authored by rikki_iki's avatar rikki_iki
Browse files

Fix tests

parent d9b62eab
......@@ -58,9 +58,6 @@ field.formatter.settings.image_class:
class:
type: string
label: 'Class for the element'
link_class:
type: string
label: 'Class for the link'
field.formatter.settings.responsive_image_class:
type: mapping
......@@ -69,9 +66,9 @@ field.formatter.settings.responsive_image_class:
image_link:
type: string
label: 'Link image to'
image_style:
responsive_image_style:
type: string
label: 'Image style'
label: 'Responsive image style'
class:
type: string
label: 'Class for the element'
......
......@@ -12,7 +12,7 @@ use Drupal\Core\Url;
* Plugin implementation of the 'file link with class' formatter.
*
* @FieldFormatter(
* id = "file_class",
* id = "file_link_class",
* label = @Translation("File link (with class)"),
* field_types = {
* "file"
......
......@@ -26,11 +26,11 @@ class LinkListClassFormatter extends LinkFormatter {
* {@inheritdoc}
*/
public static function defaultSettings() {
$defaut_settings = parent::defaultSettings() + [
$default_settings = parent::defaultSettings() + [
'list_type' => 'ul',
];
return ElementClassTrait::linkClassDefaultSettings($defaut_settings);
return ElementClassTrait::elementClassDefaultSettings($default_settings);
}
/**
......@@ -50,7 +50,7 @@ class LinkListClassFormatter extends LinkFormatter {
'#default_value' => $this->getSetting('list_type'),
];
return $this->linkClassSettingsForm($elements, $class);
return $this->elementClassSettingsForm($elements, $class);
}
/**
......@@ -63,7 +63,7 @@ class LinkListClassFormatter extends LinkFormatter {
$summary[] = $this->t('List type: @tag', ['@tag' => $tag]);
}
return $this->linkClassSettingsSummary($summary, $class);
return $this->elementClassSettingsSummary($summary, $class);
}
/**
......
......@@ -26,6 +26,8 @@ use Drupal\Core\Link;
*/
class WrapperClassFormatter extends FormatterBase {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
......
......@@ -4,11 +4,13 @@ namespace Drupal\Tests\element_class_formatter\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -19,11 +21,13 @@ abstract class ElementClassFormatterTestBase extends BrowserTestBase {
use ContentTypeCreationTrait;
use UserCreationTrait;
use NodeCreationTrait;
use TestFileCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'element_class_formatter',
'entity_test',
'field',
'file',
......@@ -36,14 +40,14 @@ abstract class ElementClassFormatterTestBase extends BrowserTestBase {
'telephone',
'text',
];
/**
* {@inheritdoc}
*/
protected $runAgainstInstalledSite = TRUE;
/**
* Creates a file field and set's the correct formatter.
* Creates a field and set's the correct formatter.
*
* @param string $formatter
* The formatter ID.
......@@ -72,7 +76,14 @@ abstract class ElementClassFormatterTestBase extends BrowserTestBase {
]);
$field_config->save();
$display = entity_get_display('entity_test', 'entity_test', 'full');
$values = [
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'full',
'status' => TRUE,
];
$display = EntityViewDisplay::create($values);
$display->setComponent($field_name, [
'type' => $formatter,
'settings' => $formatter_settings,
......@@ -87,7 +98,6 @@ abstract class ElementClassFormatterTestBase extends BrowserTestBase {
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->createUser(['view test entity']));
$this->createContentType(['type' => 'referenced_content']);
}
}
......@@ -9,7 +9,7 @@ use Drupal\entity_test\Entity\EntityTest;
*
* @group element_class_formatter
*/
class MailToClassFormatterTest extends ElementClassFormatterTestBase {
class EmailLinkClassFormatterTest extends ElementClassFormatterTestBase {
const TEST_CLASS = 'test-mailto-class';
......@@ -17,7 +17,7 @@ class MailToClassFormatterTest extends ElementClassFormatterTestBase {
* {@inheritdoc}
*/
public function testClassFormatter() {
$field_config = $this->createEntityField('email_mailto_class', 'email', ['class' => self::TEST_CLASS]);
$field_config = $this->createEntityField('email_link_class', 'email', ['class' => self::TEST_CLASS]);
$entity = EntityTest::create([
$field_config->getName() => [['value' => 'test@example.com']],
......
......@@ -17,7 +17,11 @@ class EntityReferenceLabelClassFormatterTest extends ElementClassFormatterTestBa
* {@inheritdoc}
*/
public function testClassFormatter() {
$field_config = $this->createEntityField('entity_reference_label_class', 'entity_reference', ['class' => self::TEST_CLASS]);
$formatter_settings = [
'class' => self::TEST_CLASS,
'tag' => 'div',
];
$field_config = $this->createEntityField('entity_reference_label_class', 'entity_reference', $formatter_settings);
$referenced_node = $this->drupalCreateNode(['type' => 'referenced_content']);
$entity = EntityTest::create([
......
......@@ -19,9 +19,10 @@ class EntityReferenceListLabelClassFormatterTest extends ElementClassFormatterTe
public function testClassFormatter() {
$formatter_settings = [
'class' => self::TEST_CLASS,
'tag' => 'ol',
'link' => TRUE,
'list_type' => 'ol',
];
$field_config = $this->createEntityField('entity_reference_label_class', 'entity_reference', $formatter_settings);
$field_config = $this->createEntityField('entity_reference_list_label_class', 'entity_reference', $formatter_settings);
$referenced_node = $this->drupalCreateNode(['type' => 'referenced_content']);
$entity = EntityTest::create([
......
......@@ -4,14 +4,13 @@ namespace Drupal\Tests\element_class_formatter\Functional;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\file\Entity\File;
use Drupal\Tests\file\Functional\Formatter\FileMediaFormatterTestBase;
/**
* Functional tests for the file link with class formatter.
*
* @group element_class_formatter
*/
class FileClassFormatterTest extends FileMediaFormatterTestBase {
class FileLinkClassFormatterTest extends ElementClassFormatterTestBase {
const TEST_CLASS = 'test-file-class';
......@@ -19,12 +18,15 @@ class FileClassFormatterTest extends FileMediaFormatterTestBase {
* {@inheritdoc}
*/
public function testClassFormatter() {
$field_config = $this->createMediaField('file_class', 'pdf', ['class' => self::TEST_CLASS]);
$formatter_settings = [
'class' => self::TEST_CLASS,
];
$field_config = $this->createEntityField('file_link_class', 'file', $formatter_settings);
file_put_contents('public://file.pdf', str_repeat('t', 10));
file_put_contents('public://file.txt', str_repeat('t', 10));
$file = File::create([
'uri' => 'public://file.pdf',
'filename' => 'file.pdf',
'uri' => 'public://file.txt',
'filename' => 'file.txt',
]);
$file->save();
......
......@@ -4,14 +4,13 @@ namespace Drupal\Tests\element_class_formatter\Functional;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\file\Entity\File;
use Drupal\Tests\file\Functional\Formatter\FileMediaFormatterTestBase;
/**
* Functional tests for the image link with class formatter.
*
* @group element_class_formatter
*/
class ImageClassFormatterTest extends FileMediaFormatterTestBase {
class ImageClassFormatterTest extends ElementClassFormatterTestBase {
const TEST_CLASS = 'test-image-class';
......@@ -19,9 +18,12 @@ class ImageClassFormatterTest extends FileMediaFormatterTestBase {
* {@inheritdoc}
*/
public function testClassFormatter() {
$field_config = $this->createMediaField('image_class', 'png', ['class' => self::TEST_CLASS]);
$formatter_settings = [
'class' => self::TEST_CLASS,
];
$field_config = $this->createEntityField('image_class', 'image', $formatter_settings);
$image = $this->getTestFiles('image')[0];
$image = current($this->getTestFiles('image'));
$file = File::create([
'uri' => $image->uri,
'status' => 1,
......
......@@ -19,9 +19,9 @@ class LinkListClassFormatterTest extends ElementClassFormatterTestBase {
public function testClassFormatter() {
$formatter_settings = [
'class' => self::TEST_CLASS,
'tag' => 'ol',
'list_type' => 'ol',
];
$field_config = $this->createEntityField('link_class', 'link', $formatter_settings);
$field_config = $this->createEntityField('link_list_class', 'link', $formatter_settings);
$entity = EntityTest::create([
$field_config->getName() => [
......
......@@ -4,14 +4,13 @@ namespace Drupal\Tests\element_class_formatter\Functional;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\file\Entity\File;
use Drupal\Tests\file\Functional\Formatter\FileMediaFormatterTestBase;
/**
* Functional tests for the responsive image link with class formatter.
*
* @group element_class_formatter
*/
class ResponsiveImageClassFormatterTest extends FileMediaFormatterTestBase {
class ResponsiveImageClassFormatterTest extends ElementClassFormatterTestBase {
const TEST_CLASS = 'test-responsive-img-class';
......@@ -19,9 +18,12 @@ class ResponsiveImageClassFormatterTest extends FileMediaFormatterTestBase {
* {@inheritdoc}
*/
public function testClassFormatter() {
$field_config = $this->createMediaField('responsive_image_class', 'png', ['class' => self::TEST_CLASS]);
$formatter_settings = [
'class' => self::TEST_CLASS,
];
$field_config = $this->createEntityField('responsive_image_class', 'image', $formatter_settings);
$image = $this->getTestFiles('image')[0];
$image = current($this->getTestFiles('image'));
$file = File::create([
'uri' => $image->uri,
'status' => 1,
......
......@@ -19,7 +19,7 @@ class StringListClassFormatterTest extends ElementClassFormatterTestBase {
public function testClassFormatter() {
$formatter_settings = [
'class' => self::TEST_CLASS,
'tag' => 'ol',
'list_type' => 'ol',
];
$field_config = $this->createEntityField('string_list_class', 'string', $formatter_settings);
......
......@@ -9,7 +9,7 @@ use Drupal\entity_test\Entity\EntityTest;
*
* @group element_class_formatter
*/
class TelephoneClassFormatterTest extends ElementClassFormatterTestBase {
class TelephoneLinkClassFormatterTest extends ElementClassFormatterTestBase {
const TEST_CLASS = 'test-phone-class';
......
<?php
namespace Drupal\Tests\element_class_formatter\Functional;
use Drupal\entity_test\Entity\EntityTest;
/**
* Functional tests for the mailto link with class formatter.
*
* @group element_class_formatter
*/
class WrapperClassFormatterTest extends ElementClassFormatterTestBase {
const TEST_CLASS = 'test-wrapper-class';
/**
* {@inheritdoc}
*/
public function testClassFormatter() {
$formatter_settings = [
'class' => self::TEST_CLASS,
'tag' => 'h2',
];
$field_config = $this->createEntityField('wrapper_class', 'string', $formatter_settings);
$entity = EntityTest::create([
$field_config->getName() => [['value' => 'I am a string']],
]);
$entity->save();
$this->drupalGet($entity->toUrl());
$assert_session = $this->assertSession();
$assert_session->elementExists('css', 'h2.' . self::TEST_CLASS);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment