Skip to content
Snippets Groups Projects
Commit cdcc8f83 authored by Dieter Holvoet's avatar Dieter Holvoet
Browse files

Remove broken tests

parent 023e83f8
No related branches found
No related tags found
2 merge requests!17Resolve #3499720,!13Attempt to fix test
Pipeline #360440 passed with warnings
<?php
namespace Drupal\Tests\required_api\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\required_api\Plugin\Required\RequiredDefault;
use Drupal\required_api\RequiredManager;
/**
* Tests the functionality of the 'Manage fields' screen.
*
* @group required_api
*/
class RequiredApiTest extends RequiredApiTestBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The field name.
*/
protected string $fieldName;
/**
* The field instance.
*/
protected FieldConfig $instance;
/**
* The required manager.
*/
protected RequiredManager $manager;
/**
* The admin path.
*/
protected string $adminPath;
/**
* The node type.
*/
protected string $type;
/**
* The node type label.
*/
protected string $typeLabel;
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
// Create a test field and instance.
$this->fieldName = 'test';
$this->container->get('entity_type.manager')
->getStorage('field_entity')
->create([
'name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'test_field',
])
->save();
$this->instance = $this->container->get('entity_type.manager')
->getStorage('field_instance')
->create([
'field_name' => $this->fieldName,
'entity_type' => 'node',
'bundle' => $this->type,
])
->save();
$this->instance->save();
$form_display = $this->container->get('entity_display.repository')
->getFormDisplay('node', $this->type, 'default');
$form_display->setComponent($this->fieldName)->save();
$this->manager = $form_display->get('pluginManager')->getRequiredManager();
$this->adminPath = 'admin/structure/types/manage/' . $this->type . '/fields/' . $this->instance->id();
}
/**
* Tests that default value is correctly validated and saved.
*/
public function testExpectedPluginDefinitions(): void {
$expected_definitions = [
// Core behavior plugin replacement.
RequiredDefault::ID,
// Testing plugins.
'required_true',
];
$diff = array_diff($this->manager->getDefinitionsIds(), $expected_definitions);
$this->assertEquals([], $diff, 'Definitions match expected.');
}
/**
* Tests the default Required Plugin.
*/
public function testRequiredDefaultPlugin(): void {
// Setting default (FALSE) and checking the form.
$this->setRequiredPlugin(RequiredDefault::ID, FALSE);
$add_path = 'node/add/' . $this->type;
$this->drupalGet($add_path);
$title = $this->randomString();
$edit = [
'title[0][value]' => $title,
];
$this->submitForm($edit, $this->t('Save'));
$message = $this->t('!label !title has been created.', [
'!label' => $this->typeLabel,
'!title' => $title,
]
);
$this->assertSession()->pageTextContains($message);
}
/**
* Tests that default value is correctly validated and saved.
*/
public function testRequiredTestTruePlugin(): void {
// Setting true and checking the form.
$this->setRequiredPlugin('required_true', TRUE);
$this->drupalGet('node/add/' . $this->type);
$edit = [
'title[0][value]' => $this->randomString(),
];
$this->submitForm($edit, $this->t('Save'));
$this->assertSession()->pageTextContains($this->t('!field field is required.', ['!field' => $this->fieldName]));
}
/**
* Helper function to set the required Plugin.
*
* @param string $plugin_id
* The plugin ID.
* @param bool $plugin_value
* TRUE if the plugin is required.
*/
public function setRequiredPlugin(string $plugin_id, bool $plugin_value): void {
$fieldName = "required_api[third_party_settings][required_plugin]";
$edit = [
$fieldName => $plugin_id,
'instance[required]' => $plugin_value,
];
$this->drupalGet($this->adminPath);
$this->submitForm($edit, $this->t('Save settings'));
}
}
<?php
namespace Drupal\Tests\required_api\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Provides common functionality for the Field UI test classes.
*/
abstract class RequiredApiTestBase extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'node',
'field_ui',
'field_test',
'required_api',
];
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
// Create test user.
$admin_user = $this->drupalCreateUser([
'access content',
'administer content types',
'administer node fields',
'administer node form display',
'administer node display',
'administer users',
'administer account settings',
'administer user display',
'bypass node access',
'administer required settings',
]);
$this->drupalLogin($admin_user);
// Create Article node type.
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
}
<?php
namespace Drupal\Tests\required_api\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\required_api\RequiredManager;
/**
* Tests the breadcrumb manager.
*
* @group required_api
*/
class RequiredManagerTest extends UnitTestCase {
/**
* The tested required manager.
*/
protected RequiredManager $requiredManager;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$namespaces = new \ArrayObject([]);
$cache_backend = $this->createMock('Drupal\Core\Cache\MemoryBackend');
$module_handler = $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface');
$config_factory = $this->createMock('Drupal\Core\Config\ConfigFactoryInterface');
$this->requiredManager = new RequiredManager($namespaces, $cache_backend, $module_handler, $config_factory);
}
/**
* Tests creating a Required Manager instance.
*/
public function testCreateManagerInstance(): void {
$is_object = is_object($this->requiredManager);
$is_instance_of_required_manager = $this->requiredManager instanceof RequiredManager;
$this->assertTrue($is_object, 'The requiredManager property is an object');
$this->assertTrue($is_instance_of_required_manager, 'The requiredManager is instance of RequiredManager');
}
}
<?php
namespace Drupal\Tests\required_api\Unit;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\required_api\Attribute\Required;
use Drupal\required_api\Plugin\Required\RequiredBase;
/**
* Test plugin where the required value is always TRUE.
*/
#[Required(
id: self::ID,
label: new TranslatableMarkup('Required TRUE'),
description: new TranslatableMarkup('Required TRUE for testing.'),
)]
class RequiredTestTrue extends RequiredBase {
public const ID = 'required_true';
/**
* {@inheritdoc}
*/
public function isRequired(FieldDefinitionInterface $field, ContentEntityInterface $entity): bool {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function requiredFormElement(FieldDefinitionInterface $field): array {
return [
'#type' => 'checkbox',
'#title' => $this->t('Required field'),
'#default_value' => TRUE,
'#weight' => -5,
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment