Merge branch 'MDL-62950-master' of git://github.com/andrewnicols/moodle
[moodle.git] / admin / tool / policy / tests / behat / behat_tool_policy.php
CommitLineData
cf398020
MG
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Step definition for tool_policy
19 *
20 * @package tool_policy
21 * @category test
22 * @copyright 2018 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
27
28use Behat\Gherkin\Node\TableNode as TableNode;
29
30/**
31 * Step definition for tool_policy
32 *
33 * @package tool_policy
34 * @category test
35 * @copyright 2018 Marina Glancy
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class behat_tool_policy extends behat_base {
39
40 /**
41 * Click on an entry in the edit menu.
42 *
43 * @Given /^the following policies exist:$/
44 *
673b5981
MG
45 * Supported table fields:
46 *
47 * - Name: Policy name (required).
48 * - Revision: Revision name (policy version).
49 * - Status: Policy version status - 'draft', 'active' or 'archived'. Defaults to 'active'.
50 * - Audience: Target users - 'guest', 'all' or 'loggedin'. Default to 'all'.
51 * - Type: 0 - site policy, 1 - privacy policy, 2 - third party policy, 99 - other.
52 * - Summary: Policy summary text.
53 * - Content: Policy full text.
54 *
cf398020
MG
55 * @param TableNode $data
56 */
57 public function the_following_policies_exist(TableNode $data) {
58 global $CFG;
59 if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
60 throw new Exception('Site policy handler is not set to "tool_policy"');
61 }
62
63 $fields = [
673b5981
MG
64 'name',
65 'revision',
66 'policy',
67 'status',
68 'audience',
69 'type',
cf398020
MG
70 'content',
71 'summary',
72 ];
73
74 // Associative array "policy identifier" => id in the database .
75 $policies = [];
76
77 foreach ($data->getHash() as $elementdata) {
78 $data = (object)[
79 'audience' => \tool_policy\policy_version::AUDIENCE_ALL,
80 'archived' => 0,
81 'type' => 0
82 ];
83 $elementdata = array_change_key_case($elementdata, CASE_LOWER);
84 foreach ($elementdata as $key => $value) {
85 if ($key === 'policy') {
86 if (array_key_exists($value, $policies)) {
87 $data->policyid = $policies[$value];
88 }
89 } else if ($key === 'status') {
90 $data->archived = ($value === 'archived');
91 } else if ($key === 'audience') {
92 if ($value === 'guest') {
93 $data->audience = \tool_policy\policy_version::AUDIENCE_GUESTS;
94 } else if ($value === 'loggedin') {
95 $data->audience = \tool_policy\policy_version::AUDIENCE_LOGGEDIN;
96 }
97 } else if (($key === 'summary' || $key === 'content') && !empty($value)) {
98 $data->{$key.'_editor'} = ['text' => $value, 'format' => FORMAT_MOODLE];
99 } else if (in_array($key, $fields) && $value !== '') {
100 $data->$key = $value;
101 }
102 }
103 if (empty($data->name) || empty($data->content_editor) || empty($data->summary_editor)) {
104 throw new Exception('Policy is missing at least one of the required fields: name, content, summary');
105 }
106
107 if (!empty($data->policyid)) {
108 $version = tool_policy\api::form_policydoc_update_new($data);
109 } else {
110 $version = \tool_policy\api::form_policydoc_add($data);
111 }
112
113 if (!empty($elementdata['policy'])) {
114 $policies[$elementdata['policy']] = $version->get('policyid');
115 }
116 if (empty($elementdata['status']) || $elementdata['status'] === 'active') {
117 \tool_policy\api::make_current($version->get('id'));
118 }
119 }
120 }
121}