AutoEntityLabelBatchTest.php in Automatic Entity Label 8.3
File
tests/src/FunctionalJavascript/AutoEntityLabelBatchTest.php
View source
<?php
namespace Drupal\Tests\auto_entitylabel\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class AutoEntityLabelBatchTest extends WebDriverTestBase {
protected $nodeType;
protected $configFactory;
protected $user;
protected $nodeStorage;
public static $modules = [
'system',
'user',
'node',
'filter',
'token',
'auto_entitylabel',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this->user = $this
->drupalCreateUser([], '', TRUE);
$this
->drupalLogin($this->user);
$this->nodeType = $this
->createContentType([
'type' => 'page',
]);
$this->configFactory = $this->container
->get('config.factory');
$this->nodeStorage = $this->container
->get('entity_type.manager')
->getStorage('node');
}
public function testBatchProcess() {
$webAssert = $this
->assertSession();
$this
->createTestNodes(10, 'page');
$pagesIDs = $this->nodeStorage
->getQuery()
->condition('type', 'page')
->execute();
foreach ($this->nodeStorage
->loadMultiple($pagesIDs) as $index => $page) {
$this
->assertEquals('Testing node page ' . ($index - 1), $page
->get('title')->value);
$page
->save();
}
$this->configFactory
->getEditable("auto_entitylabel.settings.node.{$this->nodeType->id()}")
->set('status', 1)
->set('pattern', 'Test node [current-user:account-name]')
->save();
$this
->drupalGet('/admin/structure/types/manage/page/auto-label');
$webAssert
->pageTextContains('AUTOMATIC LABEL GENERATION FOR');
$webAssert
->fieldExists('status');
$webAssert
->fieldExists('pattern');
$webAssert
->fieldExists('save');
$webAssert
->fieldExists('chunk');
$edit = [
'save' => TRUE,
'chunk' => 5,
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$webAssert
->assertWaitOnAjaxRequest();
$webAssert
->pageTextContains('The configuration options have been saved.');
$webAssert
->pageTextContains('Resaved 10 labels.');
foreach ($this->nodeStorage
->loadMultiple($pagesIDs) as $page) {
$this
->assertEquals('Test node ' . $this->user
->getAccountName(), $page
->get('title')->value);
}
}
public function createTestNodes(int $numberOfNodes, string $nodeType) {
for ($i = 0; $i < $numberOfNodes; $i++) {
$this
->drupalCreateNode([
'type' => $nodeType,
'title' => 'Testing node ' . $nodeType . ' ' . $i,
]);
}
}
}