X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fmetatag%2Ftests%2Fsrc%2FFunctional%2FMetatagHelperTrait.php;fp=web%2Fmodules%2Fcontrib%2Fmetatag%2Ftests%2Fsrc%2FFunctional%2FMetatagHelperTrait.php;h=e885355108001ae24d3083f80f8d7f79af542658;hp=6d63315f235f247d2ccbfb855104b0f8e1e3dfc0;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/modules/contrib/metatag/tests/src/Functional/MetatagHelperTrait.php b/web/modules/contrib/metatag/tests/src/Functional/MetatagHelperTrait.php index 6d63315f2..e88535510 100644 --- a/web/modules/contrib/metatag/tests/src/Functional/MetatagHelperTrait.php +++ b/web/modules/contrib/metatag/tests/src/Functional/MetatagHelperTrait.php @@ -2,9 +2,12 @@ namespace Drupal\Tests\metatag\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; - /** * Misc helper functions for the automated tests. */ @@ -14,15 +17,141 @@ trait MetatagHelperTrait { * Log in as user 1. */ protected function loginUser1() { - // Log in as user 1. + // 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); } + /** + * {@inheritdoc} + */ + protected function verbose($message, $title = NULL) { + // Handle arrays, objects, etc. + if (!is_string($message)) { + $message = "
\n" . print_r($message, TRUE) . "\n
\n"; + } + + // Optional title to go before the output. + if (!empty($title)) { + $title = '

' . Html::escape($title) . "

\n"; + } + + parent::verbose($title . $message); + } + + /** + * 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. + * + * @return \Drupal\node\NodeInterface + * A fully formatted node object. + */ + private function createContentTypeNode($title = 'Title test', $body = 'Body test') { + $content_type = 'metatag_test'; + $args = [ + 'type' => $content_type, + 'label' => 'Test content type', + ]; + $this->createContentType($args); + + $args = [ + 'body' => [ + [ + 'value' => $body, + 'format' => filter_default_format(), + ], + ], + 'title' => $title, + 'type' => $content_type, + ]; + + return $this->createNode($args); + } + + /** + * 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\Entity\Vocabulary + * A fully formatted vocabulary object. + */ + private 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(); + + 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; + } + + /** + * Create a taxonomy term. + * + * @param array $values + * Items passed to the term. Requires the 'vid' element. + * + * @return Drupal\taxonomy\Entity\Term + * A fully formatted term object. + */ + private function createTerm(array $values = []) { + // Populate defaults array. + $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; + } + }