Mercurial > hg > isophonics-drupal-site
diff core/modules/block_content/src/Tests/BlockContentTestBase.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/block_content/src/Tests/BlockContentTestBase.php Wed Nov 29 16:09:58 2017 +0000 @@ -0,0 +1,116 @@ +<?php + +namespace Drupal\block_content\Tests; + +use Drupal\block_content\Entity\BlockContent; +use Drupal\block_content\Entity\BlockContentType; +use Drupal\simpletest\WebTestBase; + +/** + * Sets up block content types. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use \Drupal\Tests\block_content\Functional\BlockContentTestBase instead. + */ +abstract class BlockContentTestBase extends WebTestBase { + + /** + * Profile to use. + */ + protected $profile = 'testing'; + + /** + * Admin user + * + * @var \Drupal\user\UserInterface + */ + protected $adminUser; + + /** + * Permissions to grant admin user. + * + * @var array + */ + protected $permissions = [ + 'administer blocks' + ]; + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = ['block', 'block_content']; + + /** + * Whether or not to auto-create the basic block type during setup. + * + * @var bool + */ + protected $autoCreateBasicBlockType = TRUE; + + /** + * Sets the test up. + */ + protected function setUp() { + parent::setUp(); + if ($this->autoCreateBasicBlockType) { + $this->createBlockContentType('basic', TRUE); + } + + $this->adminUser = $this->drupalCreateUser($this->permissions); + $this->drupalPlaceBlock('local_actions_block'); + } + + /** + * Creates a custom block. + * + * @param bool|string $title + * (optional) Title of block. When no value is given uses a random name. + * Defaults to FALSE. + * @param string $bundle + * (optional) Bundle name. Defaults to 'basic'. + * @param bool $save + * (optional) Whether to save the block. Defaults to TRUE. + * + * @return \Drupal\block_content\Entity\BlockContent + * Created custom block. + */ + protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) { + $title = $title ?: $this->randomMachineName(); + $block_content = BlockContent::create([ + 'info' => $title, + 'type' => $bundle, + 'langcode' => 'en' + ]); + if ($block_content && $save === TRUE) { + $block_content->save(); + } + return $block_content; + } + + /** + * Creates a custom block type (bundle). + * + * @param string $label + * The block type label. + * @param bool $create_body + * Whether or not to create the body field + * + * @return \Drupal\block_content\Entity\BlockContentType + * Created custom block type. + */ + protected function createBlockContentType($label, $create_body = FALSE) { + $bundle = BlockContentType::create([ + 'id' => $label, + 'label' => $label, + 'revision' => FALSE, + ]); + $bundle->save(); + if ($create_body) { + block_content_add_body_field($bundle->id()); + } + return $bundle; + } + +}