View source
<?php
namespace Drupal\Tests\auto_entitylabel\Kernel;
use Drupal\auto_entitylabel\AutoEntityLabelManager;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class AutoEntityLabelTest extends EntityKernelTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
protected $nodeType;
protected $configFactory;
public static $modules = [
'system',
'user',
'node',
'filter',
'token',
'auto_entitylabel',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installSchema('user', 'users_data');
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig(self::$modules);
$this->nodeType = $this
->createContentType([
'type' => 'page',
]);
$this->configFactory = $this->container
->get('config.factory');
}
public function testDisabledOption() {
$this
->setConfiguration([
'status' => AutoEntityLabelManager::DISABLED,
]);
$user = $this
->createUser();
$title = 'Test Node';
$node = $this
->createNode([
'title' => $title,
'uid' => $user
->id(),
'type' => $this->nodeType
->id(),
]);
$this
->assertEquals($title, $node
->getTitle(), 'The title is untouched.');
}
public function testEnabledOption() {
$this
->setConfiguration([
'status' => AutoEntityLabelManager::ENABLED,
'pattern' => '[node:author:name]',
]);
$user = $this
->createUser();
$node = $this
->createNode([
'uid' => $user
->id(),
'type' => $this->nodeType
->id(),
]);
$this
->assertEquals($user
->getAccountName(), $node
->getTitle(), 'The title is set.');
}
public function testOptionalOption() {
$this
->setConfiguration([
'status' => AutoEntityLabelManager::OPTIONAL,
'pattern' => '[node:author:name]',
]);
$user = $this
->createUser();
$title = 'Test Node';
$node = $this
->createNode([
'title' => $title,
'uid' => $user
->id(),
'type' => $this->nodeType
->id(),
]);
$this
->assertEquals($title, $node
->getTitle(), 'The title is untouched.');
$node = $this
->createNode([
'title' => '',
'uid' => $user
->id(),
'type' => $this->nodeType
->id(),
]);
$this
->assertEquals($user
->getAccountName(), $node
->getTitle(), 'The title is set.');
}
public function testEscapeOption() {
$this
->setConfiguration([
'status' => AutoEntityLabelManager::ENABLED,
'pattern' => '[node:author:name] -?;: testing 123 ><*+',
'escape' => TRUE,
]);
$user = $this
->createUser();
$node = $this
->createNode([
'uid' => $user
->id(),
'type' => $this->nodeType
->id(),
]);
$this
->assertEquals($user
->getAccountName() . ' testing 123', $node
->getTitle(), 'The title is set.');
}
public function setConfiguration(array $params) {
$autoEntityLabelSettings = $this->configFactory
->getEditable("auto_entitylabel.settings.node.{$this->nodeType->id()}");
foreach ($params as $key => $value) {
$autoEntityLabelSettings
->set($key, $value);
}
$autoEntityLabelSettings
->save();
}
}