added example for developpers module

This commit is contained in:
Bachir Soussi Chiadmi
2018-01-06 11:28:03 +01:00
parent 5017966908
commit 5ec2f4311c
396 changed files with 26299 additions and 0 deletions
@@ -0,0 +1,13 @@
name: 'Sample Description Template Test'
type: module
description: 'Support module for testing the DescriptionTemplateTrait.'
package: Testing
# core: 8.x
dependencies:
- examples
# Information added by Drupal.org packaging script on 2017-12-17
version: '8.x-1.x-dev'
core: '8.x'
project: 'examples'
datestamp: 1513537386
@@ -0,0 +1,6 @@
example_description_trait_test.description:
path: 'examples/tests/example-description'
defaults:
_controller: '\Drupal\examples_description_test\Controller\SampleExampleController::description'
requirements:
_permission: 'access content'
@@ -0,0 +1,34 @@
<?php
namespace Drupal\examples_description_test\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\examples\Utility\DescriptionTemplateTrait;
/**
* Simple controller class used to test the DescriptionTemplateTrait.
*/
class SampleExampleController extends ControllerBase {
use DescriptionTemplateTrait;
/**
* {@inheritdoc}
*/
protected function getModuleName() {
return 'examples_description_test';
}
/**
* {@inheritdoc}
*
* We override this so we can see some substitutions.
*/
protected function getDescriptionVariables() {
$variables = [
'module' => $this->getModuleName(),
'slogan' => $this->t('We aim to please'),
];
return $variables;
}
}
@@ -0,0 +1,25 @@
{*
Description template test example.
This is a test template that demonstrates how an Examples module can
implement its description controller by using a Twig template.
*}
<!-
Template loaded!
-->
{% trans %}
<h2>Sample Description</h2>
<p>Here is a sample description. It embeds some Twig variables.</p>
<ol>
<li>Used in module: {{ module }}.</li>
<li>Our slogan for today: {{slogan}}.</li>
</ol>
{% endtrans %}
@@ -0,0 +1,47 @@
<?php
namespace Drupal\Tests\examples\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* A standardized base class for Examples tests.
*
* Use this base class if the Examples module being tested requires menus, local
* tasks, and actions.
*/
abstract class ExamplesBrowserTestBase extends BrowserTestBase {
/**
* Modules to install.
*
* @var array
*/
public static $modules = ['examples', 'block'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Add the system menu blocks to appropriate regions.
$this->setupExamplesMenus();
}
/**
* Set up menus and tasks in their regions.
*
* Since menus and tasks are now blocks, we're required to explicitly set them
* to regions. This method standardizes the way we do that for Examples.
*
* Note that subclasses must explicitly declare that the block module is a
* dependency.
*/
protected function setupExamplesMenus() {
$this->drupalPlaceBlock('system_menu_block:tools', ['region' => 'primary_menu']);
$this->drupalPlaceBlock('local_tasks_block', ['region' => 'secondary_menu']);
$this->drupalPlaceBlock('local_actions_block', ['region' => 'content']);
$this->drupalPlaceBlock('page_title_block', ['region' => 'content']);
}
}
@@ -0,0 +1,74 @@
<?php
namespace Drupal\Tests\examples\Functional;
/**
* Minimal test case for the examples module.
*
* @group examples
*
* @ingroup examples
*/
class ExamplesTest extends ExamplesBrowserTestBase {
/**
* Modules to install.
*
* @var array
*/
public static $modules = ['examples', 'toolbar'];
/**
* Test whether the module was installed.
*/
public function testExamples() {
$assert = $this->assertSession();
// Verify that the toolbar tab and tray are showing and functioning.
$user = $this->drupalCreateUser(['access toolbar']);
$this->drupalLogin($user);
// Check for the 'Examples' tab.
$this->drupalGet('');
$assert->statusCodeEquals(200);
// Assert that the toolbar tab registered by examples is present.
$assert->linkExists('Examples');
// Assert that the toolbar tab registered by examples is present.
$this->assertEquals(
1,
\count($this->xpath('//nav/div/a[@data-toolbar-tray="toolbar-item-examples-tray"]')),
'Found the Examples toolbar tab.'
);
// Assert that the toolbar tray registered by examples is present.
$this->assertEquals(
1,
\count($this->xpath('//nav/div/div[@data-toolbar-tray="toolbar-item-examples-tray"]')),
'Found the Examples toolbar tray.'
);
// Assert that PHPUnit link does not appears in the tray.
$phpunit_link = 'PHPUnit Example';
$assert->linkNotExists($phpunit_link);
$assert->pageTextNotContains('<li class="phpunit-example">');
// Install phpunit_example and see if it appears in the toolbar. We use
// phpunit_example because it's very light-weight.
$this->container->get('module_installer')->install(['phpunit_example'], TRUE);
// SimpleTest needs for us to reset all the caches.
$this->resetAll();
// Verify that PHPUnit appears in the tray.
$this->drupalGet('');
$assert->linkExists($phpunit_link);
// Assert that the PHPUnit tray item is present.
$this->assertEquals(
1,
\count($this->xpath('//nav/div/div/nav/ul/li[@class="phpunit-example"]')),
'Found the PHPUnit Example tray item.'
);
}
}
@@ -0,0 +1,41 @@
<?php
namespace Drupal\Tests\examples\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\examples_description_test\Controller\SampleExampleController;
/**
* Test of the Description Trait.
*
* @group examples
*/
class DescriptionTraitTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['examples', 'examples_description_test'];
/**
* Make sure that the trait finds the template file and renders it.
*/
public function testTemplateFile() {
$sample_controller = SampleExampleController::create($this->container);
// We want to test ::getDescriptionTemplatePath(), which is a protected
// method. Use a little of the Old Black Reflection Magic.
$ref_get_path = new \ReflectionMethod($sample_controller, 'getDescriptionTemplatePath');
$ref_get_path->setAccessible(TRUE);
$this->assertFileExists($ref_get_path->invoke($sample_controller));
// And get our render output.
$render_array = $sample_controller->description();
// We cast to string, since renderPlain() returns a markup object.
$output = (string) $this->container->get('renderer')->renderPlain($render_array);
// Did the template load?
$this->assertContains('Template loaded!', $output);
// Were the variables resolved correctly?
$this->assertContains('Used in module: examples_description_test.', $output);
$this->assertContains('Our slogan for today: We aim to please.', $output);
}
}
@@ -0,0 +1,49 @@
<?php
namespace Drupal\Tests\examples\Unit;
use Symfony\Component\Yaml\Yaml;
/**
* Validate requirements for config YAML.
*
* YAML in modules' config/ directory should not have a uuid: key. We'll use
* this test to check whether that's the case.
*
* @group examples
*/
class YamlValidationTest extends \PHPUnit_Framework_TestCase {
/**
* Find all the config YAML files and provide them to the test.
*
* @return array[]
* An array of arrays of strings, suitable as a data provider. Strings are
* paths to YAML files in config directories.
*/
public function provideYamls() {
$yaml_paths = [];
$examples_project_path = realpath(__DIR__ . '/../../..');
$paths = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($examples_project_path, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
foreach ($paths as $path) {
$pathname = $path->getPathname();
if (strpos($pathname, '.yml') !== FALSE) {
if (strpos($pathname, '/config/') !== FALSE) {
$yaml_paths[] = [$pathname];
}
}
}
return $yaml_paths;
}
/**
* @dataProvider provideYamls
*/
public function testNoUuidsInConfig($yaml_path) {
$yaml = Yaml::parse(file_get_contents($yaml_path));
$this->assertArrayNotHasKey('uuid', $yaml, "YAML in this file contains a uuid key: $yaml_path");
}
}