Security update for permissions_by_term
[yaffs-website] / vendor / drupal / drupal-extension / doc / contexts.rst
1 Contexts
2 ========
3
4 Before Behat 3, each test suite was limited to a single context class. As of
5 Behat 3, it is possible to flexibly structure your code by using multiple
6 contexts in a single test suite.
7
8 Available Contexts
9 ------------------
10
11 In accordance with this new capability, The Drupal Extension includes the
12 following contexts:
13
14 *RawDrupalContext*
15   A context that provides no step definitions, but all of the
16   necessary functionality for interacting with Drupal, and with the
17   browser via Mink sessions.
18
19 *DrupalContext*
20   Provides step-definitions for creating users, terms, and nodes.
21
22 *MinkContext*
23   Builds on top of the Mink Extension and adds steps specific to regions and
24   forms.
25
26 *MarkupContext*
27   Contains step definitions that deal with low-level markup (such as tags,
28   classes, and attributes).
29
30 *MessageContext*
31   Step-definitions that are specific to Drupal messages that get displayed
32   (notice, warning, and error).
33
34 *DrushContext*
35   Allows steps to directly call drush commands.
36
37 Custom Contexts
38 ---------------
39
40 You can structure your own code with additional contexts. See Behat's `testing features <http://docs.behat.org/en/latest/guides/4.contexts.html>`_ documentation for a detailed discussion of how contexts work.
41
42 .. Important::
43
44    Every context you want to use in a suite must declare it in the behat.yml
45    file.
46
47 Example
48 #######
49
50 In this example, you would have access to:
51
52  * pre-written step definitions for users, terms, and nodes
53    (from the ``DrupalContext``)
54  * steps you've implemented in the  main
55    ``features/bootstrap/FeatureContext.php`` file
56  * steps you've implemented in the ``CustomContext`` class
57
58 You would not have access to the steps from the ``MarkupContext``,
59 ``MessageContext``, or ``DrushContext``, however.
60
61 .. code-block:: yaml
62    :linenos:
63
64     default:
65       suites:
66         default:
67           contexts:
68             - Drupal\DrupalExtension\Context\DrupalContext
69             - FeatureContext
70             - CustomContext
71
72 Context communication
73 ---------------------
74
75 Since Behat 3 can have many concurrent contexts active, communication between those  contexts can be important.
76
77 The following will gather any specified contexts before a given scenario is run:
78
79   .. literalinclude:: _static/snippets/context-communication.inc
80      :language: php
81      :linenos:
82
83 Drupal Extension Hooks
84 ----------------------
85
86 In addition to the `hooks provided by Behat
87 <http://behat.readthedocs.org/en/v2.5/guides/3.hooks.html>`_, the Drupal
88 Extension provides three additional ways to tag the methods in your
89 ``CustomContext`` class in order to have them fire before certain events.
90
91   1. ``@beforeNodeCreate``
92   2. ``@beforeTermCreate``
93   3. ``@beforeUserCreate``
94
95 Example
96 #######
97
98 .. code-block:: php
99    :linenos:
100
101      use Drupal\DrupalExtension\Hook\Scope\EntityScope;
102       ...
103       /**
104        * Call this function before nodes are created.
105        *
106        * @beforeNodeCreate
107        */
108        public function alterNodeObject(EntityScope $scope) {
109          $node = $scope->getEntity();
110          // Alter node object as needed.
111        }
112