weekly release 2.6dev
[moodle.git] / cache / stores / session / tests / session_test.php
CommitLineData
ac6754a6
SH
1<?php
2// This session 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 * Session unit tests
19 *
20 * @package cachestore_session
21 * @copyright 2013 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27// Include the necessary evils.
28global $CFG;
29require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
30require_once($CFG->dirroot.'/cache/stores/session/lib.php');
31
32/**
33 * Session unit test class.
34 *
35 * @package cachestore_session
36 * @copyright 2013 Sam Hemelryk
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class cachestore_session_test extends cachestore_tests {
40 /**
41 * Returns the session class name
42 * @return string
43 */
44 protected function get_class_name() {
45 return 'cachestore_session';
46 }
cb359009
SH
47
48 /**
49 * Test the maxsize option.
50 */
51 public function test_maxsize() {
5ebe37fb 52 $defid = 'phpunit/testmaxsize';
cb359009 53 $config = cache_config_phpunittest::instance();
5ebe37fb 54 $config->phpunit_add_definition($defid, array(
cb359009
SH
55 'mode' => cache_store::MODE_SESSION,
56 'component' => 'phpunit',
5ebe37fb 57 'area' => 'testmaxsize',
cb359009
SH
58 'maxsize' => 3
59 ));
5ebe37fb
DW
60 $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
61 $instance = cachestore_session::initialise_test_instance($definition);
cb359009 62
5ebe37fb
DW
63 $this->assertTrue($instance->set('key1', 'value1'));
64 $this->assertTrue($instance->set('key2', 'value2'));
65 $this->assertTrue($instance->set('key3', 'value3'));
cb359009 66
5ebe37fb
DW
67 $this->assertTrue($instance->has('key1'));
68 $this->assertTrue($instance->has('key2'));
69 $this->assertTrue($instance->has('key3'));
cb359009 70
5ebe37fb
DW
71 $this->assertTrue($instance->set('key4', 'value4'));
72 $this->assertTrue($instance->set('key5', 'value5'));
cb359009 73
5ebe37fb
DW
74 $this->assertFalse($instance->has('key1'));
75 $this->assertFalse($instance->has('key2'));
76 $this->assertTrue($instance->has('key3'));
77 $this->assertTrue($instance->has('key4'));
78 $this->assertTrue($instance->has('key5'));
2e9d5a53 79
5ebe37fb
DW
80 $this->assertFalse($instance->get('key1'));
81 $this->assertFalse($instance->get('key2'));
82 $this->assertEquals('value3', $instance->get('key3'));
83 $this->assertEquals('value4', $instance->get('key4'));
84 $this->assertEquals('value5', $instance->get('key5'));
cb359009
SH
85
86 // Test adding one more.
5ebe37fb
DW
87 $this->assertTrue($instance->set('key6', 'value6'));
88 $this->assertFalse($instance->get('key3'));
cb359009
SH
89
90 // Test reducing and then adding to make sure we don't lost one.
5ebe37fb
DW
91 $this->assertTrue($instance->delete('key6'));
92 $this->assertTrue($instance->set('key7', 'value7'));
93 $this->assertEquals('value4', $instance->get('key4'));
cb359009 94
67da5999
SH
95 // Set the same key three times to make sure it doesn't count overrides.
96 for ($i = 0; $i < 3; $i++) {
5ebe37fb 97 $this->assertTrue($instance->set('key8', 'value8'));
67da5999 98 }
5ebe37fb 99 $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size');
67da5999 100
cb359009 101 // Test adding many.
5ebe37fb
DW
102 $this->assertEquals(3, $instance->set_many(array(
103 array('key' => 'keyA', 'value' => 'valueA'),
104 array('key' => 'keyB', 'value' => 'valueB'),
105 array('key' => 'keyC', 'value' => 'valueC')
cb359009
SH
106 )));
107 $this->assertEquals(array(
108 'key4' => false,
109 'key5' => false,
110 'key6' => false,
111 'key7' => false,
112 'keyA' => 'valueA',
113 'keyB' => 'valueB',
114 'keyC' => 'valueC'
5ebe37fb 115 ), $instance->get_many(array(
cb359009
SH
116 'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
117 )));
118 }
ac6754a6 119}