View source
<?php
namespace Drupal\Tests\default_content\Functional;
use Drupal\Core\Config\FileStorage;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\user\Entity\User;
use Drupal\Tests\BrowserTestBase;
class DefaultContentTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
public static $modules = [
'taxonomy',
'hal',
'default_content',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
User::create([
'uid' => 2,
'uuid' => 'ab301be5-7017-4ff8-b2d3-09dc0a30bd43',
'name' => 'User 2',
'mail' => 'user2@example.com',
'status' => TRUE,
])
->save();
$this
->createContentType([
'type' => 'page',
]);
}
public function testImport() {
\Drupal::service('module_installer')
->install([
'default_content_test',
], TRUE);
$this
->rebuildContainer();
$this
->doPostInstallTests();
}
public function testImportViaConfigImporter() {
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($this->container
->get('config.storage'), $sync);
$extensions = $sync
->read('core.extension');
$extensions['module']['default_content_test'] = 0;
$extensions['module'] = module_config_sort($extensions['module']);
$sync
->write('core.extension', $extensions);
$module_storage = new FileStorage(drupal_get_path('module', 'default_content_test') . '/config/install');
foreach ($module_storage
->listAll() as $name) {
$sync
->write($name, $module_storage
->read($name));
}
$this
->configImporter()
->import();
$this
->doPostInstallTests();
}
protected function doPostInstallTests() {
$this
->drupalLogin($this
->drupalCreateUser([], NULL, TRUE));
$node = $this
->getNodeByTitle('Imported node');
$this
->assertEquals($node->body->value, 'Crikey it works!');
$this
->assertEquals($node
->getType(), 'page');
$this
->assertSame('2', $node
->getOwnerId(), 'The node created is owned by user 2');
$node = $this
->getNodeByTitle('Imported node with owned by user 1');
$this
->assertSame('1', $node
->getOwnerId(), 'The node created is owned by user 1');
$node = $this
->getNodeByTitle('Imported node with owned by user that does not exist');
$this
->assertSame('1', $node
->getOwnerId(), 'The node created is owned by user 1');
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadMultiple();
$term = reset($terms);
$this
->assertNotEmpty($term);
$this
->assertEquals($term->name->value, 'A tag');
$term_id = $node->field_tags->target_id;
$this
->assertNotEmpty($term_id);
}
}