scanner-8.x-1.0-rc3/tests/src/Functional/ScannerTestBase.php
tests/src/Functional/ScannerTestBase.php
<?php
namespace Drupal\Tests\scanner\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\link\LinkItemInterface;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
use Drupal\user\Entity\User;
/**
* Helper test class with some added functions for testing.
*/
abstract class ScannerTestBase extends BrowserTestBase {
use ParagraphsTestBaseTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'node',
'link',
'paragraphs',
'views',
'scanner',
];
/**
* Log in as user 1.
*/
protected function loginUser1(): void {
// Load user 1.
/** @var \Drupal\user\Entity\User $account */
$account = User::load(1);
// Reset the password.
$password = 'foo';
$account->setPassword($password)->save();
// Support old and new tests.
$account->passRaw = $password;
$account->pass_raw = $password;
// Login.
$this->drupalLogin($account);
}
/**
* Create a vocabulary.
*
* @param array $values
* Items passed to the vocabulary. If the 'vid' item is not present it will
* be automatically generated. If the 'name' item is not present the 'vid'
* will be used.
*
* @return \Drupal\taxonomy\VocabularyInterface|\Drupal\Core\Entity\EntityInterface
* A fully formatted vocabulary object.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createVocabulary(array $values = []) {
// Find a non-existent random type name.
if (!isset($values['vid'])) {
do {
$id = strtolower($this->randomMachineName(8));
} while (Vocabulary::load($id));
}
else {
$id = $values['vid'];
}
$values += [
'vid' => $id,
'name' => $id,
];
$vocab = Vocabulary::create($values);
$status = $vocab->save();
$this->assertEquals(SAVED_NEW, $status, (new FormattableMarkup('Created vocabulary %type.', ['%type' => $vocab->id()]))->__toString());
return $vocab;
}
/**
* Create a taxonomy term.
*
* @param array $values
* Items passed to the term. Requires the 'vid' element.
*
* @return \Drupal\taxonomy\TermInterface|\Drupal\Core\Entity\EntityInterface
* A fully formatted term object.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createTerm(array $values = []): TermInterface|EntityInterface {
// Populate defaults array.
$values += [
'description' => [
[
'value' => $this->randomMachineName(32),
'format' => filter_default_format(),
],
],
'name' => $this->randomMachineName(8),
];
$term = Term::create($values);
$status = $term->save();
$this->assertEquals(SAVED_NEW, $status, (new FormattableMarkup('Created term %name.', ['%name' => $term->label()]))->__toString());
return $term;
}
/**
* Create a content type and a node.
*
* @param string $title
* A title for the node that will be returned.
* @param string $body
* The text to use as the body.
* @param string $content_type
* The node bundle type.
* @param string $content_type_label
* The content type label.
*
* @return \Drupal\node\NodeInterface
* A fully formatted node object.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createContentTypeNode(string $title, string $body, string $content_type, string $content_type_label): NodeInterface {
$args = [
'type' => $content_type,
'name' => $content_type_label,
];
$this->createContentType($args);
$fieldStorage = FieldStorageConfig::load('node.link_field');
$field_name = 'link_field';
if (is_null($fieldStorage)) {
// Create a field with settings to validate.
$fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'link',
]);
$fieldStorage->save();
}
$linkField = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => $content_type,
'settings' => [
'title' => DRUPAL_DISABLED,
'link_type' => LinkItemInterface::LINK_GENERIC,
],
]);
$linkField->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getFormDisplay('node', $content_type)
->setComponent($field_name, [
'type' => 'link_default',
])->save();
$display_repository->getViewDisplay('node', $content_type)
->setComponent($field_name, [
'type' => 'link',
])->save();
$args = [
'body' => [
[
'value' => $body,
'format' => filter_default_format(),
],
],
$field_name => [
[
'uri' => 'https://example.com',
],
],
'title' => $title,
'type' => $content_type,
];
return $this->createNode($args);
}
}
