ScannerHelperTrait.php in Search and Replace Scanner 8
File
tests/src/Functional/ScannerHelperTrait.php
View source
<?php
namespace Drupal\Tests\scanner\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
trait ScannerHelperTrait {
protected function loginUser1() {
$account = User::load(1);
$password = 'foo';
$account
->setPassword($password)
->save();
$account->passRaw = $password;
$account->pass_raw = $password;
$this
->drupalLogin($account);
}
protected function verbose($message, $title = NULL) {
if (!is_string($message)) {
$message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
}
if (!empty($title)) {
$title = '<h2>' . Html::escape($title) . "</h2>\n";
}
parent::verbose($title . $message);
}
protected function createVocabulary(array $values = []) {
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();
if ($this instanceof \PHPUnit_Framework_TestCase) {
$this
->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', [
'%type' => $vocab
->id(),
]))
->__toString());
}
else {
$this
->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', [
'%type' => $vocab
->id(),
]))
->__toString());
}
return $vocab;
}
protected function createTerm(array $values = []) {
$values += [
'description' => [
[
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
],
],
'name' => $this
->randomMachineName(8),
];
$term = Term::create($values);
$status = $term
->save();
if ($this instanceof \PHPUnit_Framework_TestCase) {
$this
->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', [
'%name' => $term
->label(),
]))
->__toString());
}
else {
$this
->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', [
'%name' => $term
->label(),
]))
->__toString());
}
return $term;
}
}