Skip to content

Instantly share code, notes, and snippets.

@kazukomurata
Created December 3, 2021 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazukomurata/27222594b9da425719eb59537181f6eb to your computer and use it in GitHub Desktop.
Save kazukomurata/27222594b9da425719eb59537181f6eb to your computer and use it in GitHub Desktop.
Drupal Kernelテストのサンプル
<?php
namespace Drupal\Tests\example_module\Kernel;
use Drupal\Core\Config\FileStorage;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\ConfigTestTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
* Provides example test.
*/
class ExampleTest extends KernelTestBase {
use ConfigTestTrait;
use NodeCreationTrait;
use UserCreationTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'system',
'node',
'taxonomy',
'user',
'text',
'field',
'filter', // 本文にテキストフォーマットが必要なので指定しています.
'menu_ui', // コンテンツからメニューを登録する機能を有効にしているコンテンツなので、dependencies があり指定しています.
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Kernelテストでは、$modulesに指定しても config/install や config/schema は投入されないため、
// setUpで投入します.
// filterはbodyフィールドのテキストフォーマット用で、Plain Textを投入します.
$this->installEntitySchema('node');
$this->installEntitySchema('taxonomy_term');
$this->installConfig('filter');
// configのインポートには system.siteのuuidの一致が必要なので、先に更新します.
$site_uuid = '0937caa0-2ea0-4494-8022-55f210a49c75';
$this->container->get('config.factory')->getEditable('system.site')
->set('uuid', $site_uuid)
->save();
// インポート準備.
$this->configImporter();
$active = $this->container->get('config.storage');
$sync = $this->container->get('config.storage.sync');
// ConfigTestTraitのメソッド.
// DBのconfig = $active を $sync にコピー.
$this->copyConfig($active, $sync);
// configのyamlファイルがあるディレクトリを絶対パスで指定します.
$config_path = DRUPAL_ROOT . '/../sync';
$storage = new FileStorage($config_path);
$config_list = [
'node.type.article', // 記事コンテンツのconfig.
'field.storage.node.body', // 記事コンテンツの本文.
'field.storage.node.field_tags', // 記事コンテンツのタグ(タクソノミーターム参照).
'field.field.node.article.body', // 記事コンテンツの本文のストレージ設定.
'field.field.node.article.field_tags', // 記事コンテンツのタグのストレージ設定.
'taxonomy.vocabulary.tags', // タグ タクソノミータームの設定.
];
// $sync に yaml を読み込みながら書き込みします.
// drush config:import 実行時の差分表示は、
// 書き込み後の$sync と $active を比較して表示されています.
foreach ($storage->listAll() as $config) {
/** @var \Drupal\Core\Config\StorageInterface $sync */
if (in_array($config, $config_list)) {
$sync->write($config, $storage->read($config));
}
}
// ここで$syncの内容がDBに入ります.
// drush config:import で yes した時に走る処理だと思ってください.
$this->configImporter->import();
}
/**
* Tests example.
*/
public function testExample() {
// UserCreationTraitのメソッド、createNode()がユーザを要求するので呼んでいます.
$this->setUpCurrentUser();
// タグのターム作成.
$term = Term::create(['vid' => 'tags', 'name' => 'タグテスト']);
$term->save();
// NodeCreationTrait のメソッド.
$node = $this->createNode(['type' => 'article', 'field_tags' => $term->id()]);
$this->assertTrue($node instanceof Node);
$result_terms = $node->get('field_tags')->referencedEntities();
$this->assertEquals('タグテスト', reset($result_terms)->label());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment