weekly on-sync release 3.1dev
[moodle.git] / lib / testing / generator / block_generator.php
CommitLineData
7e7cfe7a
PS
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 * Block generator base class.
19 *
20 * @package core
6b219869 21 * @category test
7e7cfe7a
PS
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
c29e3e24 26defined('MOODLE_INTERNAL') || die();
7e7cfe7a
PS
27
28/**
29 * Block generator base class.
30 *
31 * Extend in blocks/xxxx/tests/generator/lib.php as class block_xxxx_generator.
32 *
33 * @package core
6b219869 34 * @category test
7e7cfe7a
PS
35 * @copyright 2012 Petr Skoda {@link http://skodak.org}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
ba203de1 38abstract class testing_block_generator extends component_generator_base {
7e7cfe7a
PS
39 /** @var number of created instances */
40 protected $instancecount = 0;
41
7e7cfe7a
PS
42 /**
43 * To be called from data reset code only,
44 * do not use in tests.
45 * @return void
46 */
47 public function reset() {
48 $this->instancecount = 0;
49 }
50
51 /**
52 * Returns block name
53 * @return string name of block that this class describes
54 * @throws coding_exception if class invalid
55 */
56 public function get_blockname() {
57 $matches = null;
58 if (!preg_match('/^block_([a-z0-9_]+)_generator$/', get_class($this), $matches)) {
59 throw new coding_exception('Invalid block generator class name: '.get_class($this));
60 }
61
62 if (empty($matches[1])) {
63 throw new coding_exception('Invalid block generator class name: '.get_class($this));
64 }
65 return $matches[1];
66 }
67
68 /**
69 * Fill in record defaults
70 * @param stdClass $record
71 * @return stdClass
72 */
73 protected function prepare_record(stdClass $record) {
74 $record->blockname = $this->get_blockname();
75 if (!isset($record->parentcontextid)) {
76 $record->parentcontextid = context_system::instance()->id;
77 }
78 if (!isset($record->showinsubcontexts)) {
79 $record->showinsubcontexts = 1;
80 }
81 if (!isset($record->pagetypepattern)) {
82 $record->pagetypepattern = '';
83 }
84 if (!isset($record->subpagepattern)) {
85 $record->subpagepattern = null;
86 }
87 if (!isset($record->defaultregion)) {
88 $record->defaultregion = '';
89 }
90 if (!isset($record->defaultweight)) {
91 $record->defaultweight = '';
92 }
93 if (!isset($record->configdata)) {
94 $record->configdata = null;
95 }
96 return $record;
97 }
98
99 /**
100 * Create a test block
101 * @param array|stdClass $record
102 * @param array $options
103 * @return stdClass activity record
104 */
105 abstract public function create_instance($record = null, array $options = null);
106}