MDL-61864 tool_policy: user agreement reports
[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 *
45 * @param TableNode $data
46 */
47 public function the_following_policies_exist(TableNode $data) {
48 global $CFG;
49 if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
50 throw new Exception('Site policy handler is not set to "tool_policy"');
51 }
52
53 $fields = [
54 'name', // Policy name (required).
55 'revision', // Revision name.
56 'policy', // Any policy identifier, can be used to generate multiple versions of the same policy.
57 'status', // Version status: 'draft', 'active', 'archived'. By default 'active'.
58 'audience', // Audience: 'guest', 'all', 'loggedin' (by default 'all').
59 'type', // 0 (default) - Site policy, 1 - Privacy policy, 2 - Third party policy, 99 - Other .
60 'content',
61 'summary',
62 ];
63
64 // Associative array "policy identifier" => id in the database .
65 $policies = [];
66
67 foreach ($data->getHash() as $elementdata) {
68 $data = (object)[
69 'audience' => \tool_policy\policy_version::AUDIENCE_ALL,
70 'archived' => 0,
71 'type' => 0
72 ];
73 $elementdata = array_change_key_case($elementdata, CASE_LOWER);
74 foreach ($elementdata as $key => $value) {
75 if ($key === 'policy') {
76 if (array_key_exists($value, $policies)) {
77 $data->policyid = $policies[$value];
78 }
79 } else if ($key === 'status') {
80 $data->archived = ($value === 'archived');
81 } else if ($key === 'audience') {
82 if ($value === 'guest') {
83 $data->audience = \tool_policy\policy_version::AUDIENCE_GUESTS;
84 } else if ($value === 'loggedin') {
85 $data->audience = \tool_policy\policy_version::AUDIENCE_LOGGEDIN;
86 }
87 } else if (($key === 'summary' || $key === 'content') && !empty($value)) {
88 $data->{$key.'_editor'} = ['text' => $value, 'format' => FORMAT_MOODLE];
89 } else if (in_array($key, $fields) && $value !== '') {
90 $data->$key = $value;
91 }
92 }
93 if (empty($data->name) || empty($data->content_editor) || empty($data->summary_editor)) {
94 throw new Exception('Policy is missing at least one of the required fields: name, content, summary');
95 }
96
97 if (!empty($data->policyid)) {
98 $version = tool_policy\api::form_policydoc_update_new($data);
99 } else {
100 $version = \tool_policy\api::form_policydoc_add($data);
101 }
102
103 if (!empty($elementdata['policy'])) {
104 $policies[$elementdata['policy']] = $version->get('policyid');
105 }
106 if (empty($elementdata['status']) || $elementdata['status'] === 'active') {
107 \tool_policy\api::make_current($version->get('id'));
108 }
109 }
110 }
111}