outline-8.x-1.x-dev/tests/src/Traits/OutlineTestTrait.php
tests/src/Traits/OutlineTestTrait.php
<?php
namespace Drupal\Tests\outline\Traits;
use Drupal\Core\Language\LanguageInterface;
use Drupal\outline\Entity\Outline;
use Drupal\outline\Entity\Entry;
use Drupal\outline\OutlineInterface;
/**
* Provides common helper methods for Outline module tests.
*/
trait OutlineTestTrait {
/**
* Returns a new outline with random properties.
*
* @return \Drupal\outline\OutlineInterface
* A outline used for testing.
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function createOutline() {
$outline = Outline::create([
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
'oid' => mb_strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'weight' => mt_rand(0, 10),
]);
/* @var $outline \Drupal\outline\OutlineInterface */
$outline->save();
return $outline;
}
/**
* Returns a new entry with random properties given a outline.
*
* @param \Drupal\outline\OutlineInterface $outline
* The outline object.
* @param array $values
* (optional) An array of values to set, keyed by property name.
*
* @return \Drupal\outline\EntryInterface
* The new outline entry object.
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function createEntry(OutlineInterface $outline, $values = []) {
$entry = Entry::create($values + [
'name' => $this->randomMachineName(),
'description' => [
'value' => $this->randomMachineName(),
// Use the fallback text format.
'format' => filter_fallback_format(),
],
'oid' => $outline->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
/* @var $entry \Drupal\outline\EntryInterface */
$entry->save();
return $entry;
}
}
