| 1 | efrain | 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 | namespace cachestore_static;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use cache_definition;
 | 
        
           |  |  | 20 | use cache_store;
 | 
        
           |  |  | 21 | use cachestore_static;
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | // Include the necessary evils.
 | 
        
           |  |  | 26 | global $CFG;
 | 
        
           |  |  | 27 | require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
 | 
        
           |  |  | 28 | require_once($CFG->dirroot.'/cache/stores/static/lib.php');
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | /**
 | 
        
           |  |  | 31 |  * Static unit test class.
 | 
        
           |  |  | 32 |  *
 | 
        
           |  |  | 33 |  * @package    cachestore_static
 | 
        
           |  |  | 34 |  * @copyright  2013 Sam Hemelryk
 | 
        
           |  |  | 35 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 36 |  */
 | 
        
           |  |  | 37 | class store_test extends \cachestore_tests {
 | 
        
           |  |  | 38 |     /**
 | 
        
           |  |  | 39 |      * Returns the static class name
 | 
        
           |  |  | 40 |      * @return string
 | 
        
           |  |  | 41 |      */
 | 
        
           |  |  | 42 |     protected function get_class_name() {
 | 
        
           |  |  | 43 |         return 'cachestore_static';
 | 
        
           |  |  | 44 |     }
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     /**
 | 
        
           |  |  | 47 |      * Test the maxsize option.
 | 
        
           |  |  | 48 |      */
 | 
        
           | 11 | efrain | 49 |     public function test_maxsize(): void {
 | 
        
           | 1 | efrain | 50 |         $defid = 'phpunit/testmaxsize';
 | 
        
           |  |  | 51 |         $config = \cache_config_testing::instance();
 | 
        
           |  |  | 52 |         $config->phpunit_add_definition($defid, array(
 | 
        
           |  |  | 53 |             'mode' => cache_store::MODE_REQUEST,
 | 
        
           |  |  | 54 |             'component' => 'phpunit',
 | 
        
           |  |  | 55 |             'area' => 'testmaxsize',
 | 
        
           |  |  | 56 |             'maxsize' => 3
 | 
        
           |  |  | 57 |         ));
 | 
        
           |  |  | 58 |         $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
 | 
        
           |  |  | 59 |         $instance = cachestore_static::initialise_test_instance($definition);
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |         $this->assertTrue($instance->set('key1', 'value1'));
 | 
        
           |  |  | 62 |         $this->assertTrue($instance->set('key2', 'value2'));
 | 
        
           |  |  | 63 |         $this->assertTrue($instance->set('key3', 'value3'));
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |         $this->assertTrue($instance->has('key1'));
 | 
        
           |  |  | 66 |         $this->assertTrue($instance->has('key2'));
 | 
        
           |  |  | 67 |         $this->assertTrue($instance->has('key3'));
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |         $this->assertTrue($instance->set('key4', 'value4'));
 | 
        
           |  |  | 70 |         $this->assertTrue($instance->set('key5', 'value5'));
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         $this->assertFalse($instance->has('key1'));
 | 
        
           |  |  | 73 |         $this->assertFalse($instance->has('key2'));
 | 
        
           |  |  | 74 |         $this->assertTrue($instance->has('key3'));
 | 
        
           |  |  | 75 |         $this->assertTrue($instance->has('key4'));
 | 
        
           |  |  | 76 |         $this->assertTrue($instance->has('key5'));
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         $this->assertFalse($instance->get('key1'));
 | 
        
           |  |  | 79 |         $this->assertFalse($instance->get('key2'));
 | 
        
           |  |  | 80 |         $this->assertEquals('value3', $instance->get('key3'));
 | 
        
           |  |  | 81 |         $this->assertEquals('value4', $instance->get('key4'));
 | 
        
           |  |  | 82 |         $this->assertEquals('value5', $instance->get('key5'));
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         // Test adding one more.
 | 
        
           |  |  | 85 |         $this->assertTrue($instance->set('key6', 'value6'));
 | 
        
           |  |  | 86 |         $this->assertFalse($instance->get('key3'));
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 |         // Test reducing and then adding to make sure we don't lost one.
 | 
        
           |  |  | 89 |         $this->assertTrue($instance->delete('key6'));
 | 
        
           |  |  | 90 |         $this->assertTrue($instance->set('key7', 'value7'));
 | 
        
           |  |  | 91 |         $this->assertEquals('value4', $instance->get('key4'));
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |         // Set the same key three times to make sure it doesn't count overrides.
 | 
        
           |  |  | 94 |         for ($i = 0; $i < 3; $i++) {
 | 
        
           |  |  | 95 |             $this->assertTrue($instance->set('key8', 'value8'));
 | 
        
           |  |  | 96 |         }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |         $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size');
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |         // Test adding many.
 | 
        
           |  |  | 101 |         $this->assertEquals(3, $instance->set_many(array(
 | 
        
           |  |  | 102 |             array('key' => 'keyA', 'value' => 'valueA'),
 | 
        
           |  |  | 103 |             array('key' => 'keyB', 'value' => 'valueB'),
 | 
        
           |  |  | 104 |             array('key' => 'keyC', 'value' => 'valueC')
 | 
        
           |  |  | 105 |         )));
 | 
        
           |  |  | 106 |         $this->assertEquals(array(
 | 
        
           |  |  | 107 |             'key4' => false,
 | 
        
           |  |  | 108 |             'key5' => false,
 | 
        
           |  |  | 109 |             'key6' => false,
 | 
        
           |  |  | 110 |             'key7' => false,
 | 
        
           |  |  | 111 |             'keyA' => 'valueA',
 | 
        
           |  |  | 112 |             'keyB' => 'valueB',
 | 
        
           |  |  | 113 |             'keyC' => 'valueC'
 | 
        
           |  |  | 114 |         ), $instance->get_many(array(
 | 
        
           |  |  | 115 |             'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
 | 
        
           |  |  | 116 |         )));
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |     /**
 | 
        
           |  |  | 120 |      * Simple test to verify igbinary availability and check basic serialization is working ok.
 | 
        
           |  |  | 121 |      */
 | 
        
           | 11 | efrain | 122 |     public function test_igbinary_serializer(): void {
 | 
        
           | 1 | efrain | 123 |         // Skip if igbinary is not available.
 | 
        
           |  |  | 124 |         if (!extension_loaded('igbinary')) {
 | 
        
           |  |  | 125 |             $this->markTestSkipped('Cannot test igbinary serializer. Extension missing');
 | 
        
           |  |  | 126 |         }
 | 
        
           |  |  | 127 |         // Prepare the static instance.
 | 
        
           |  |  | 128 |         $defid = 'phpunit/igbinary';
 | 
        
           |  |  | 129 |         $config = \cache_config_testing::instance();
 | 
        
           |  |  | 130 |         $config->phpunit_add_definition($defid, array(
 | 
        
           |  |  | 131 |             'mode' => cache_store::MODE_REQUEST,
 | 
        
           |  |  | 132 |             'component' => 'phpunit',
 | 
        
           |  |  | 133 |             'area' => 'testigbinary'
 | 
        
           |  |  | 134 |         ));
 | 
        
           |  |  | 135 |         $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
 | 
        
           |  |  | 136 |         $instance = cachestore_static::initialise_test_instance($definition);
 | 
        
           |  |  | 137 |         // Prepare an object.
 | 
        
           |  |  | 138 |         $obj = new \stdClass();
 | 
        
           |  |  | 139 |         $obj->someint = 9;
 | 
        
           |  |  | 140 |         $obj->somestring = '99';
 | 
        
           |  |  | 141 |         $obj->somearray = [9 => 999, '99' => '9999'];
 | 
        
           |  |  | 142 |         // Serialize and set.
 | 
        
           |  |  | 143 |         $objser = igbinary_serialize($obj);
 | 
        
           |  |  | 144 |         $instance->set('testigbinary', $objser);
 | 
        
           |  |  | 145 |         // Get and unserialize.
 | 
        
           |  |  | 146 |         $res = $instance->get('testigbinary');
 | 
        
           |  |  | 147 |         $resunser = igbinary_unserialize($res);
 | 
        
           |  |  | 148 |         // Check expectations.
 | 
        
           |  |  | 149 |         $this->assertSame($objser, $res);     // Ok from cache (ig-serialized, 100% same string).
 | 
        
           |  |  | 150 |         $this->assertEquals($obj, $resunser); // Ok ig-unserialized (equal
 | 
        
           |  |  | 151 |         $this->assertNotSame($obj, $resunser);// but different objects, obviously).
 | 
        
           |  |  | 152 |     }
 | 
        
           |  |  | 153 | }
 |