added example for developpers module
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- field.field.node.simpletest_example.body
|
||||
- node.type.simpletest_example
|
||||
module:
|
||||
- path
|
||||
- text
|
||||
enforced:
|
||||
module:
|
||||
- simpletest_example
|
||||
id: node.simpletest_example.default
|
||||
targetEntityType: node
|
||||
bundle: simpletest_example
|
||||
mode: default
|
||||
content:
|
||||
title:
|
||||
type: string_textfield
|
||||
weight: -5
|
||||
settings:
|
||||
size: 60
|
||||
placeholder: ''
|
||||
third_party_settings: { }
|
||||
uid:
|
||||
type: entity_reference_autocomplete
|
||||
weight: 5
|
||||
settings:
|
||||
match_operator: CONTAINS
|
||||
size: 60
|
||||
placeholder: ''
|
||||
third_party_settings: { }
|
||||
created:
|
||||
type: datetime_timestamp
|
||||
weight: 10
|
||||
settings: { }
|
||||
third_party_settings: { }
|
||||
promote:
|
||||
type: boolean_checkbox
|
||||
weight: 15
|
||||
settings:
|
||||
display_label: true
|
||||
third_party_settings: { }
|
||||
sticky:
|
||||
type: boolean_checkbox
|
||||
weight: 16
|
||||
settings:
|
||||
display_label: true
|
||||
third_party_settings: { }
|
||||
path:
|
||||
type: path
|
||||
weight: 30
|
||||
settings: { }
|
||||
third_party_settings: { }
|
||||
body:
|
||||
type: text_textarea_with_summary
|
||||
weight: 31
|
||||
settings:
|
||||
rows: 9
|
||||
summary_rows: 3
|
||||
placeholder: ''
|
||||
third_party_settings: { }
|
||||
hidden: { }
|
||||
third_party_settings: { }
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.node.body
|
||||
- node.type.simpletest_example
|
||||
module:
|
||||
- text
|
||||
enforced:
|
||||
module:
|
||||
- simpletest_example
|
||||
id: node.simpletest_example.body
|
||||
field_name: body
|
||||
entity_type: node
|
||||
bundle: simpletest_example
|
||||
label: Body
|
||||
description: ''
|
||||
required: false
|
||||
translatable: true
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings:
|
||||
display_summary: false
|
||||
third_party_settings: { }
|
||||
field_type: text_with_summary
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
enforced:
|
||||
module:
|
||||
- simpletest_example
|
||||
name: 'SimpleTest Example Node Type'
|
||||
type: simpletest_example
|
||||
description: 'A content type that exists so we can test it.'
|
||||
help: ''
|
||||
new_revision: false
|
||||
preview_mode: 1
|
||||
display_submitted: true
|
||||
third_party_settings: { }
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
name: "SimpleTest Example Mock Module"
|
||||
type: module
|
||||
hidden: true
|
||||
description: "Mock module for the SimpleTest Example module."
|
||||
package: Example modules
|
||||
# core: 8.x
|
||||
dependencies:
|
||||
- simpletest_example
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-12-17
|
||||
version: '8.x-1.x-dev'
|
||||
core: '8.x'
|
||||
project: 'examples'
|
||||
datestamp: 1513537386
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Implements simpletest_example_test module.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
|
||||
/**
|
||||
* The mock module for SimpleTest Example.
|
||||
*
|
||||
* This module exists so that we can enable it and use it to
|
||||
* test elements of simpletest_module.
|
||||
*
|
||||
* @ingroup simpletest_example
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_ENTITY_TYPE_view().
|
||||
*
|
||||
* We'll just add some content to nodes of the type we like.
|
||||
*
|
||||
* @ingroup simpletest_example
|
||||
*/
|
||||
function simpletest_example_test_node_view(
|
||||
array &$build,
|
||||
EntityInterface $node,
|
||||
EntityViewDisplayInterface $display,
|
||||
$view_mode) {
|
||||
if ($node->getType() == 'simpletest_example') {
|
||||
$build['simpletest_example_test_section'] = [
|
||||
'#markup' => t('The test module did its thing.'),
|
||||
'#weight' => -99,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
name: SimpleTest Example
|
||||
type: module
|
||||
hidden: false
|
||||
description: 'Demonstrates some SimpleTest-based tests in Drupal 8.'
|
||||
package: Example modules
|
||||
# core: 8.x
|
||||
# We have to be as explicit as possible about the dependencies for this module.
|
||||
# If you look at the config info in the config/install/ directory, you'll see
|
||||
# that they depend on some of these modules.
|
||||
dependencies:
|
||||
- drupal:simpletest
|
||||
- drupal:node
|
||||
- drupal:field
|
||||
- drupal:path
|
||||
- drupal:text
|
||||
- examples: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
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
simpletest_example.description:
|
||||
title: SimpleTest Example
|
||||
route_name: simpletest_example_description
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Module file for simpletest_example.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
/**
|
||||
* @defgroup simpletest_example Example: SimpleTest
|
||||
* @ingroup examples
|
||||
* @{
|
||||
*
|
||||
* An example of how to write functional tests using SimpleTest under
|
||||
* Drupal 8.
|
||||
*
|
||||
* This module creates a new node type called 'SimpleTest Example Node Type,'
|
||||
* so that we can test it.
|
||||
*
|
||||
* This code was originally written to accompany the tutorial at
|
||||
* http://drupal.org/node/890654. That's a Drupal 7 example, but can still
|
||||
* teach you much.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_node_access().
|
||||
*
|
||||
* Demonstrates a bug that we'll find in our test.
|
||||
*
|
||||
* If this is running on the testbot, we don't want the error to show so will
|
||||
* work around it by testing to see if we're in the 'checkout' directory.
|
||||
*/
|
||||
function simpletest_example_node_access(NodeInterface $node, $op, AccountInterface $account) {
|
||||
// Gather the node type.
|
||||
$type = $node->getType();
|
||||
// If it's not a simpletest_example node, or if it's not operations we care
|
||||
// about, then just ignore.
|
||||
if ($type != 'simpletest_example' || ($op != 'update' && $op != 'delete')) {
|
||||
return AccessResult::neutral();
|
||||
}
|
||||
|
||||
// This code has a BUG that we'll find in testing.
|
||||
//
|
||||
// This is the incorrect version we'll use to demonstrate test failure.
|
||||
// The correct version should have ($op == 'update' || $op == 'delete').
|
||||
// The author had mistakenly always tested with User 1 so it always
|
||||
// allowed access and the bug wasn't noticed!
|
||||
if (($op == 'delete') && ($account->hasPermission('extra special edit any simpletest_example') && ($account->id() == $node->getAuthorId()))) {
|
||||
return AccessResult::allowed();
|
||||
}
|
||||
|
||||
return AccessResult::forbidden();
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "defgroup simpletest_example".
|
||||
*/
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
# In this case we're adding an addition permission that does the same
|
||||
# as the one the node module offers, just to demonstrate this error.
|
||||
'extra special edit any simpletest_example':
|
||||
title: Extra special edit any SimpleTest Example
|
||||
description: Allow user to edit any SimpleTest Example content authored by any user.
|
||||
'restrict access': TRUE
|
||||
@@ -0,0 +1,8 @@
|
||||
# This module only has one route.
|
||||
# It is to a page explaining the module.
|
||||
simpletest_example_description:
|
||||
path: 'examples/simpletest-example'
|
||||
defaults:
|
||||
_controller: '\Drupal\simpletest_example\Controller\SimpleTestExampleController::description'
|
||||
requirements:
|
||||
_permission: 'access content'
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\simpletest_example\Controller;
|
||||
|
||||
use Drupal\examples\Utility\DescriptionTemplateTrait;
|
||||
|
||||
/**
|
||||
* Controller for Simpletest description page.
|
||||
*/
|
||||
class SimpleTestExampleController {
|
||||
|
||||
use DescriptionTemplateTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getModuleName() {
|
||||
return 'simpletest_example';
|
||||
}
|
||||
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\simpletest_example\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Demonstrate SimpleTest with a mock module.
|
||||
*
|
||||
* SimpleTestExampleMockModuleTestCase allows us to demonstrate how you can
|
||||
* use a mock module to aid in functional testing in Drupal.
|
||||
*
|
||||
* If you have some functionality that's not intrinsic to the code under test,
|
||||
* you can add a special mock module that only gets installed during test
|
||||
* time. This allows you to implement APIs created by your module, or otherwise
|
||||
* exercise the code in question.
|
||||
*
|
||||
* This test case class is very similar to SimpleTestExampleTestCase. The main
|
||||
* difference is that we enable the simpletest_example_test module by providing
|
||||
* it in the $modules property. Then we can test for behaviors provided by that
|
||||
* module.
|
||||
*
|
||||
* @see SimpleTestExampleTestCase
|
||||
*
|
||||
* @ingroup simpletest_example
|
||||
*
|
||||
* SimpleTest uses group annotations to help you organize your tests.
|
||||
*
|
||||
* @group simpletest_example
|
||||
* @group examples
|
||||
*/
|
||||
class SimpleTestExampleMockModuleTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Our module dependencies.
|
||||
*
|
||||
* In Drupal 8's SimpleTest, we declare module dependencies in a public
|
||||
* static property called $modules.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static public $modules = [
|
||||
'simpletest_example',
|
||||
'simpletest_example_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* Test modifications made by our mock module.
|
||||
*
|
||||
* We create a simpletest_example node and then see if our submodule
|
||||
* operated on it.
|
||||
*/
|
||||
public function testSimpleTestExampleMockModule() {
|
||||
// Create a user.
|
||||
$test_user = $this->drupalCreateUser(['access content']);
|
||||
// Log them in.
|
||||
$this->drupalLogin($test_user);
|
||||
// Set up some content.
|
||||
$settings = [
|
||||
'type' => 'simpletest_example',
|
||||
'title' => $this->randomMachineName(32),
|
||||
];
|
||||
// Create the content node.
|
||||
$node = $this->drupalCreateNode($settings);
|
||||
// View the node.
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
// Check that our module did it's thing.
|
||||
$this->assertText(t('The test module did its thing.'), "Found evidence of test module.");
|
||||
}
|
||||
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\simpletest_example\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Ensure that the simpletest_example content type provided functions properly.
|
||||
*
|
||||
* The SimpleTestExampleTest is a functional test case, meaning that it
|
||||
* actually exercises a particular sequence of actions through the web UI.
|
||||
* The majority of core test cases are done this way, but the SimpleTest suite
|
||||
* also provides unit tests as demonstrated in the unit test case example later
|
||||
* in this file.
|
||||
*
|
||||
* Functional test cases are far slower to execute than unit test cases because
|
||||
* they require a complete Drupal install to be done for each test.
|
||||
*
|
||||
* @see Drupal\simpletest\WebTestBase
|
||||
* @see SimpleTestUnitTestExampleTestCase
|
||||
*
|
||||
* @ingroup simpletest_example
|
||||
*
|
||||
* SimpleTest uses group annotations to help you organize your tests.
|
||||
*
|
||||
* @group simpletest_example
|
||||
* @group examples
|
||||
*/
|
||||
class SimpleTestExampleTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Our module dependencies.
|
||||
*
|
||||
* In Drupal 8's SimpleTest, we declare module dependencies in a public
|
||||
* static property called $modules. WebTestBase automatically enables these
|
||||
* modules for us.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static public $modules = ['simpletest_example'];
|
||||
|
||||
/**
|
||||
* The installation profile to use with this test.
|
||||
*
|
||||
* We use the 'minimal' profile so that there are some reasonable default
|
||||
* blocks defined, and so we can see the menu link created by our module.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $profile = 'minimal';
|
||||
|
||||
/**
|
||||
* Test SimpleTest Example menu and page.
|
||||
*
|
||||
* Enable SimpleTest Example and see if it can successfully return its main
|
||||
* page and if there is a link to the simpletest_example in the Tools menu.
|
||||
*/
|
||||
public function testSimpleTestExampleMenu() {
|
||||
// Test for a link to the simpletest_example in the Tools menu.
|
||||
$this->drupalGet('');
|
||||
$this->assertResponse(200, 'The Home page is available.');
|
||||
$this->assertLinkByHref('examples/simpletest-example');
|
||||
|
||||
// Verify that anonymous can access the simpletest_examples page.
|
||||
$this->drupalGet('examples/simpletest-example');
|
||||
$this->assertResponse(200, 'The SimpleTest Example description page is available.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test node creation through the user interface.
|
||||
*
|
||||
* Creates a node using the node/add form and verifies its consistency in
|
||||
* the database.
|
||||
*/
|
||||
public function testSimpleTestExampleCreate() {
|
||||
// Create a user with the ability to create our content type. This
|
||||
// permission is generated by the node module.
|
||||
$user = $this->createUser(['create simpletest_example content']);
|
||||
// Log in our user.
|
||||
$this->drupalLogin($user);
|
||||
|
||||
// Create a node using the node/add form.
|
||||
$edit = [];
|
||||
$edit['title[0][value]'] = $this->randomMachineName(8);
|
||||
$edit['body[0][value]'] = $this->randomMachineName(16);
|
||||
$this->drupalPostForm('node/add/simpletest_example', $edit, 'Save');
|
||||
|
||||
// Check that our simpletest_example node has been created.
|
||||
$this->assertText(t('@post @title has been created.', [
|
||||
'@post' => 'SimpleTest Example Node Type',
|
||||
'@title' => $edit['title[0][value]'],
|
||||
]));
|
||||
// Check that the node exists in the database.
|
||||
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
||||
$this->assertTrue($node, 'Node found in database.');
|
||||
|
||||
// Verify 'submitted by' information. Drupal adds a newline in there, so
|
||||
// we have to check for that.
|
||||
$username = $this->loggedInUser->getUsername();
|
||||
$datetime = format_date($node->getCreatedTime());
|
||||
$submitted_by = "Submitted by $username\n on $datetime";
|
||||
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
$this->assertText($submitted_by);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a simpletest_example node and then see if our user can edit it.
|
||||
*
|
||||
* Note that some assertions in this test will fail. We do this to show what
|
||||
* a failing test looks like. Since we don't want this to interfere with
|
||||
* automated tests, however, we jump through some hoops to determine our
|
||||
* environment.
|
||||
*/
|
||||
public function testSimpleTestExampleEdit() {
|
||||
// Create a user with our special permission.
|
||||
$user = $this->drupalCreateUser(['extra special edit any simpletest_example']);
|
||||
// Log in our user.
|
||||
$this->drupalLogin($user);
|
||||
|
||||
// Create a node with our user as the creator.
|
||||
// drupalCreateNode() uses the logged-in user by default.
|
||||
$settings = [
|
||||
'type' => 'simpletest_example',
|
||||
'title' => $this->randomMachineName(32),
|
||||
];
|
||||
$node = $this->drupalCreateNode($settings);
|
||||
|
||||
// For debugging, we might output some information using $this->verbose()
|
||||
// It will only be output if the testing settings have 'verbose' set.
|
||||
$this->verbose('Node created: ' . $node->getTitle());
|
||||
|
||||
// This section demonstrates a failing test. However, we want this test to
|
||||
// pass when it's running on the Drupal QA testbot. So we need to determine
|
||||
// which environment we're running inside of before we continue.
|
||||
if (!$this->runningOnTestbot()) {
|
||||
$this->drupalGet('node/' . $node->id() . '/edit');
|
||||
// The debug() statement will output information into the test results.
|
||||
// It can also be used in Drupal anywhere in code and will come out
|
||||
// as a drupal_set_message().
|
||||
debug('The following test should fail. Examine the verbose message above it to see why.');
|
||||
// Make sure we don't get a 401 unauthorized response:
|
||||
$this->assertResponse(200, 'User is allowed to edit the content.');
|
||||
|
||||
// Looking for title text in the page to determine whether we were
|
||||
// successful opening edit form.
|
||||
$this->assertText(t("@title", ['@title' => $settings['title']]), "Found title in edit form");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if we're running on PIFR testbot.
|
||||
*
|
||||
* We can skip intentional failure if we're on the testbot. It happens that
|
||||
* on the testbot the site under test is in a directory named 'checkout' or
|
||||
* 'site_under_test'.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if running on testbot.
|
||||
*/
|
||||
public function runningOnTestbot() {
|
||||
// @todo: Add this line back once the testbot variable is available.
|
||||
// https://www.drupal.org/node/2565181
|
||||
// return env('DRUPALCI');
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
{#
|
||||
|
||||
Description text for the Simpletest Example.
|
||||
|
||||
#}
|
||||
|
||||
{% trans %}
|
||||
<p>Please note that the use of SimpleTest is deprecated. This example module will
|
||||
be removed in Drupal 9, and new tests should not be written using SimpleTest. In
|
||||
addition, all existing SimpleTest tests should be converted to PHPUnit
|
||||
functional tests.</p>
|
||||
|
||||
<p>
|
||||
There are some instructions for how to convert Simpletest-based tests to the new
|
||||
BrowserTestBase in this change notice:
|
||||
<a href="https://www.drupal.org/node/2469723">https://www.drupal.org/node/2469723</a>.
|
||||
</p>
|
||||
{% endtrans %}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\simpletest_example\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Functional tests of the simpletest_example module.
|
||||
*
|
||||
* @ingroup simpletest_example
|
||||
*
|
||||
* @group simpletest_example
|
||||
* @group examples
|
||||
*/
|
||||
class SimpletestExampleTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['simpletest_example'];
|
||||
|
||||
/**
|
||||
* Verify that we can uninstall and then reinstall simpletest_example.
|
||||
*
|
||||
* Since simpletest_example installs configuration objects, it needs to clean
|
||||
* up after itself. This test verifies that it does.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2841840
|
||||
*/
|
||||
public function testUninstallReinstall() {
|
||||
$session = $this->assertSession();
|
||||
|
||||
// The simpletest_example module should have been installed by the test, so
|
||||
// we can just uninstall it.
|
||||
/* @var $module_installer \Drupal\Core\Extension\ModuleInstallerInterface */
|
||||
$module_installer = $this->container->get('module_installer');
|
||||
$module_installer->uninstall(['simpletest_example']);
|
||||
$this->drupalGet('examples/simpletest-example');
|
||||
$session->statusCodeEquals(404);
|
||||
|
||||
// We reinstall the simpletest_example module to make sure it happens
|
||||
// properly.
|
||||
$module_installer->install(['simpletest_example']);
|
||||
$this->drupalGet('examples/simpletest-example');
|
||||
$session->statusCodeEquals(200);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user