diff --git a/tests/src/Functional/HelpTopicTest.php b/tests/src/Functional/HelpTopicTest.php index c318a6f..1276884 100644 --- a/tests/src/Functional/HelpTopicTest.php +++ b/tests/src/Functional/HelpTopicTest.php @@ -2,7 +2,9 @@ namespace Drupal\Tests\config_help\Functional; +use Drupal\Core\Serialization\Yaml; use Drupal\Tests\BrowserTestBase; +use Drupal\config_help\Entity\HelpTopic; /** * Verifies help topic display and user access to help based on permissions. @@ -114,6 +116,70 @@ class HelpTopicTest extends BrowserTestBase { $session->linkNotExists('ABC Help Test'); } + /** + * Tests export and import of help topics, and topic create from array. + */ + public function testTopicExportImport() { + // Create a help topic entity. + $values = [ + 'id' => 'foo', + 'label' => 'Foo', + 'body' => [ + [ + 'type' => 'heading', + 'text' => 'Greetings', + ], + [ + 'type' => 'paragraph', + 'text' => 'Hello, world!', + ], + ], + 'top_level' => TRUE, + 'locked' => TRUE, + 'related' => ['config_help'], + 'list_on' => ['config_help_form'], + ]; + // Set the dependecies after creation, because it's an odd bit of the + // configuration. + $dependencies = [ + 'module' => ['config_help', 'filter'], + 'theme' => ['classy', 'bartik'], + ]; + + /** @var \Drupal\config_help\HelpTopicInterface $foo */ + $foo = HelpTopic::create($values); + $foo->setEnforcedDependencies($dependencies); + + // Export and import the topic. + $foo_export = Yaml::encode($foo->toArray()); + $bar = HelpTopic::create(Yaml::decode($foo_export)); + + // Verify that everything is OK. + $to_check = [ + // This is an array of method name => component in $values, except + // dependencies, which are checked separately. + 'id' => 'id', + 'label' => 'label', + 'getBody' => 'body', + 'isTopLevel' => 'top_level', + 'isLocked' => 'locked', + 'getRelated' => 'related', + 'getListOn' => 'list_on', + 'getEnforcedDependencies' => FALSE, + ]; + + // Verify that the initial create got the right values, and that after + // export/import, the values are the same. + foreach ($to_check as $method => $component) { + if ($component) { + $this->assertEqual(call_user_func([$foo, $method]), $values[$component], 'Data for ' . $component . ' is the same as method ' . $method); + } + $this->assertEqual(call_user_func([$foo, $method]), call_user_func([$bar, $method]), 'Method ' . $method . ' is the same before and after export/import'); + } + $this->assertEqual($foo->getEnforcedDependencies(), $dependencies, 'Data for dependencies is the same as method getEnforcedDependencies'); + + } + /** * Verifies the logged in user has access to various help links and pages. *