PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/cache/tests/fixtures/lib.php

https://gitlab.com/JrLucena/moodle
PHP | 539 lines | 243 code | 41 blank | 255 comment | 27 complexity | fa3f87897621ca6de6e1e3bdc124c7c0 MD5 | raw file
  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. * Support library for the cache PHPUnit tests.
  18. *
  19. * This file is part of Moodle's cache API, affectionately called MUC.
  20. * It contains the components that are requried in order to use caching.
  21. *
  22. * @package core
  23. * @category cache
  24. * @copyright 2012 Sam Hemelryk
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. defined('MOODLE_INTERNAL') || die();
  28. require_once($CFG->dirroot.'/cache/locallib.php');
  29. /**
  30. * Override the default cache configuration for our own maniacal purposes.
  31. *
  32. * This class was originally named cache_config_phpunittest but was renamed in 2.9
  33. * because it is used for both unit tests and acceptance tests.
  34. *
  35. * @since 2.9
  36. * @copyright 2012 Sam Hemelryk
  37. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38. */
  39. class cache_config_testing extends cache_config_writer {
  40. /**
  41. * Creates the default configuration and saves it.
  42. *
  43. * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
  44. * be called when there is no configuration file already.
  45. *
  46. * @param bool $forcesave If set to true then we will forcefully save the default configuration file.
  47. * @return true|array Returns true if the default configuration was successfully created.
  48. * Returns a configuration array if it could not be saved. This is a bad situation. Check your error logs.
  49. */
  50. public static function create_default_configuration($forcesave = false) {
  51. global $CFG;
  52. // HACK ALERT.
  53. // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
  54. // default store plugins are protected from deletion.
  55. $writer = new self;
  56. $writer->configstores = self::get_default_stores();
  57. $writer->configdefinitions = self::locate_definitions();
  58. $defaultapplication = 'default_application';
  59. $appdefine = defined('TEST_CACHE_USING_APPLICATION_STORE') ? TEST_CACHE_USING_APPLICATION_STORE : false;
  60. if ($appdefine !== false && preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/', $appdefine)) {
  61. $expectedstore = $appdefine;
  62. $file = $CFG->dirroot.'/cache/stores/'.$appdefine.'/lib.php';
  63. $class = 'cachestore_'.$appdefine;
  64. if (file_exists($file)) {
  65. require_once($file);
  66. }
  67. if (class_exists($class) && $class::ready_to_be_used_for_testing()) {
  68. /* @var cache_store $class */
  69. $writer->configstores['test_application'] = array(
  70. 'use_test_store' => true,
  71. 'name' => 'test_application',
  72. 'plugin' => $expectedstore,
  73. 'alt' => $writer->configstores[$defaultapplication],
  74. 'modes' => $class::get_supported_modes(),
  75. 'features' => $class::get_supported_features()
  76. );
  77. $defaultapplication = 'test_application';
  78. }
  79. }
  80. $writer->configmodemappings = array(
  81. array(
  82. 'mode' => cache_store::MODE_APPLICATION,
  83. 'store' => $defaultapplication,
  84. 'sort' => -1
  85. ),
  86. array(
  87. 'mode' => cache_store::MODE_SESSION,
  88. 'store' => 'default_session',
  89. 'sort' => -1
  90. ),
  91. array(
  92. 'mode' => cache_store::MODE_REQUEST,
  93. 'store' => 'default_request',
  94. 'sort' => -1
  95. )
  96. );
  97. $writer->configlocks = array(
  98. 'default_file_lock' => array(
  99. 'name' => 'cachelock_file_default',
  100. 'type' => 'cachelock_file',
  101. 'dir' => 'filelocks',
  102. 'default' => true
  103. )
  104. );
  105. $factory = cache_factory::instance();
  106. // We expect the cache to be initialising presently. If its not then something has gone wrong and likely
  107. // we are now in a loop.
  108. if (!$forcesave && $factory->get_state() !== cache_factory::STATE_INITIALISING) {
  109. return $writer->generate_configuration_array();
  110. }
  111. $factory->set_state(cache_factory::STATE_SAVING);
  112. $writer->config_save();
  113. return true;
  114. }
  115. /**
  116. * Returns the expected path to the configuration file.
  117. *
  118. * We override this function to add handling for $CFG->altcacheconfigpath.
  119. * We want to support it so that people can run unit tests against alternative cache setups.
  120. * However we don't want to ever make changes to the file at $CFG->altcacheconfigpath so we
  121. * always use dataroot and copy the alt file there as required.
  122. *
  123. * @throws cache_exception
  124. * @return string The absolute path
  125. */
  126. protected static function get_config_file_path() {
  127. global $CFG;
  128. // We always use this path.
  129. $configpath = $CFG->dataroot.'/muc/config.php';
  130. if (!empty($CFG->altcacheconfigpath)) {
  131. // No need to check we are within a test here, this is the cache config class that gets used
  132. // only when one of those is true.
  133. if (!defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') || !TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) {
  134. // TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH has not being defined or is false, we want to use the default.
  135. return $configpath;
  136. }
  137. $path = $CFG->altcacheconfigpath;
  138. if (is_dir($path) && is_writable($path)) {
  139. // Its a writable directory, thats fine. Convert it to a file.
  140. $path = $CFG->altcacheconfigpath.'/cacheconfig.php';
  141. }
  142. if (is_readable($path)) {
  143. $directory = dirname($configpath);
  144. if ($directory !== $CFG->dataroot && !file_exists($directory)) {
  145. $result = make_writable_directory($directory, false);
  146. if (!$result) {
  147. throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Cannot create config directory. Check the permissions on your moodledata directory.');
  148. }
  149. }
  150. // We don't care that this fails but we should let the developer know.
  151. if (!is_readable($configpath) && !@copy($path, $configpath)) {
  152. debugging('Failed to copy alt cache config file to required location');
  153. }
  154. }
  155. }
  156. // We always use the dataroot location.
  157. return $configpath;
  158. }
  159. /**
  160. * Adds a definition to the stack
  161. * @param string $area
  162. * @param array $properties
  163. * @param bool $addmapping By default this method adds a definition and a mapping for that definition. You can
  164. * however set this to false if you only want it to add the definition and not the mapping.
  165. */
  166. public function phpunit_add_definition($area, array $properties, $addmapping = true) {
  167. if (!array_key_exists('overrideclass', $properties)) {
  168. switch ($properties['mode']) {
  169. case cache_store::MODE_APPLICATION:
  170. $properties['overrideclass'] = 'cache_phpunit_application';
  171. break;
  172. case cache_store::MODE_SESSION:
  173. $properties['overrideclass'] = 'cache_phpunit_session';
  174. break;
  175. case cache_store::MODE_REQUEST:
  176. $properties['overrideclass'] = 'cache_phpunit_request';
  177. break;
  178. }
  179. }
  180. $this->configdefinitions[$area] = $properties;
  181. if ($addmapping) {
  182. switch ($properties['mode']) {
  183. case cache_store::MODE_APPLICATION:
  184. $this->phpunit_add_definition_mapping($area, 'default_application', 0);
  185. break;
  186. case cache_store::MODE_SESSION:
  187. $this->phpunit_add_definition_mapping($area, 'default_session', 0);
  188. break;
  189. case cache_store::MODE_REQUEST:
  190. $this->phpunit_add_definition_mapping($area, 'default_request', 0);
  191. break;
  192. }
  193. }
  194. }
  195. /**
  196. * Removes a definition.
  197. * @param string $name
  198. */
  199. public function phpunit_remove_definition($name) {
  200. unset($this->configdefinitions[$name]);
  201. }
  202. /**
  203. * Removes the configured stores so that there are none available.
  204. */
  205. public function phpunit_remove_stores() {
  206. $this->configstores = array();
  207. }
  208. /**
  209. * Forcefully adds a file store.
  210. *
  211. * @param string $name
  212. */
  213. public function phpunit_add_file_store($name) {
  214. $this->configstores[$name] = array(
  215. 'name' => $name,
  216. 'plugin' => 'file',
  217. 'configuration' => array(
  218. 'path' => ''
  219. ),
  220. 'features' => 6,
  221. 'modes' => 3,
  222. 'mappingsonly' => false,
  223. 'class' => 'cachestore_file',
  224. 'default' => false,
  225. 'lock' => 'cachelock_file_default'
  226. );
  227. }
  228. /**
  229. * Forcefully adds a session store.
  230. *
  231. * @param string $name
  232. */
  233. public function phpunit_add_session_store($name) {
  234. $this->configstores[$name] = array(
  235. 'name' => $name,
  236. 'plugin' => 'session',
  237. 'configuration' => array(),
  238. 'features' => 14,
  239. 'modes' => 2,
  240. 'default' => true,
  241. 'class' => 'cachestore_session',
  242. 'lock' => 'cachelock_file_default',
  243. );
  244. }
  245. /**
  246. * Forcefully injects a definition => store mapping.
  247. *
  248. * This function does no validation, you should only be calling if it you know
  249. * exactly what to expect.
  250. *
  251. * @param string $definition
  252. * @param string $store
  253. * @param int $sort
  254. */
  255. public function phpunit_add_definition_mapping($definition, $store, $sort) {
  256. $this->configdefinitionmappings[] = array(
  257. 'store' => $store,
  258. 'definition' => $definition,
  259. 'sort' => (int)$sort
  260. );
  261. }
  262. /**
  263. * Overrides the default site identifier used by the Cache API so that we can be sure of what it is.
  264. *
  265. * @return string
  266. */
  267. public function get_site_identifier() {
  268. global $CFG;
  269. return $CFG->wwwroot.'phpunit';
  270. }
  271. }
  272. /**
  273. * This is a deprecated class. It has been renamed to cache_config_testing.
  274. *
  275. * This was deprecated in Moodle 2.9 but will be removed at the next major release
  276. * as it is only used during testing and its highly unlikely anyone has used this.
  277. *
  278. * @deprecated since 2.9
  279. * @copyright 2014 Sam Hemelryk
  280. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  281. */
  282. class cache_config_phpunittest extends cache_config_testing {
  283. // We can't do anything here to warn the user.
  284. // The cache can be utilised before sessions have even been started.
  285. }
  286. /**
  287. * Dummy object for testing cacheable object interface and interaction
  288. *
  289. * @copyright 2012 Sam Hemelryk
  290. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  291. */
  292. class cache_phpunit_dummy_object extends stdClass implements cacheable_object {
  293. /**
  294. * Test property 1
  295. * @var string
  296. */
  297. public $property1;
  298. /**
  299. * Test property 1
  300. * @var string
  301. */
  302. public $property2;
  303. /**
  304. * Constructor
  305. * @param string $property1
  306. * @param string $property2
  307. */
  308. public function __construct($property1, $property2) {
  309. $this->property1 = $property1;
  310. $this->property2 = $property2;
  311. }
  312. /**
  313. * Prepares this object for caching
  314. * @return array
  315. */
  316. public function prepare_to_cache() {
  317. return array($this->property1.'_ptc', $this->property2.'_ptc');
  318. }
  319. /**
  320. * Returns this object from the cache
  321. * @param array $data
  322. * @return cache_phpunit_dummy_object
  323. */
  324. public static function wake_from_cache($data) {
  325. return new cache_phpunit_dummy_object(array_shift($data).'_wfc', array_shift($data).'_wfc');
  326. }
  327. }
  328. /**
  329. * Dummy data source object for testing data source interface and implementation
  330. *
  331. * @copyright 2012 Sam Hemelryk
  332. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  333. */
  334. class cache_phpunit_dummy_datasource implements cache_data_source {
  335. /**
  336. * Returns an instance of this object for use with the cache.
  337. *
  338. * @param cache_definition $definition
  339. * @return cache_phpunit_dummy_datasource
  340. */
  341. public static function get_instance_for_cache(cache_definition $definition) {
  342. return new cache_phpunit_dummy_datasource();
  343. }
  344. /**
  345. * Loads a key for the cache.
  346. *
  347. * @param string $key
  348. * @return string
  349. */
  350. public function load_for_cache($key) {
  351. return $key.' has no value really.';
  352. }
  353. /**
  354. * Loads many keys for the cache
  355. *
  356. * @param array $keys
  357. * @return array
  358. */
  359. public function load_many_for_cache(array $keys) {
  360. $return = array();
  361. foreach ($keys as $key) {
  362. $return[$key] = $key.' has no value really.';
  363. }
  364. return $return;
  365. }
  366. }
  367. /**
  368. * PHPUnit application cache loader.
  369. *
  370. * Used to expose things we could not otherwise see within an application cache.
  371. *
  372. * @copyright 2012 Sam Hemelryk
  373. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  374. */
  375. class cache_phpunit_application extends cache_application {
  376. /**
  377. * Returns the class of the store immediately associated with this cache.
  378. * @return string
  379. */
  380. public function phpunit_get_store_class() {
  381. return get_class($this->get_store());
  382. }
  383. /**
  384. * Returns all the interfaces the cache store implements.
  385. * @return array
  386. */
  387. public function phpunit_get_store_implements() {
  388. return class_implements($this->get_store());
  389. }
  390. /**
  391. * Returns the given key directly from the static acceleration array.
  392. *
  393. * @param string $key
  394. * @return false|mixed
  395. */
  396. public function phpunit_static_acceleration_get($key) {
  397. return $this->static_acceleration_get($key);
  398. }
  399. }
  400. /**
  401. * PHPUnit session cache loader.
  402. *
  403. * Used to expose things we could not otherwise see within an session cache.
  404. *
  405. * @copyright 2012 Sam Hemelryk
  406. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  407. */
  408. class cache_phpunit_session extends cache_session {
  409. /**
  410. * Returns the class of the store immediately associated with this cache.
  411. * @return string
  412. */
  413. public function phpunit_get_store_class() {
  414. return get_class($this->get_store());
  415. }
  416. /**
  417. * Returns all the interfaces the cache store implements.
  418. * @return array
  419. */
  420. public function phpunit_get_store_implements() {
  421. return class_implements($this->get_store());
  422. }
  423. }
  424. /**
  425. * PHPUnit request cache loader.
  426. *
  427. * Used to expose things we could not otherwise see within an request cache.
  428. *
  429. * @copyright 2012 Sam Hemelryk
  430. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  431. */
  432. class cache_phpunit_request extends cache_request {
  433. /**
  434. * Returns the class of the store immediately associated with this cache.
  435. * @return string
  436. */
  437. public function phpunit_get_store_class() {
  438. return get_class($this->get_store());
  439. }
  440. /**
  441. * Returns all the interfaces the cache store implements.
  442. * @return array
  443. */
  444. public function phpunit_get_store_implements() {
  445. return class_implements($this->get_store());
  446. }
  447. }
  448. /**
  449. * Dummy overridden cache loader class that we can use to test overriding loader functionality.
  450. *
  451. * @copyright 2012 Sam Hemelryk
  452. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  453. */
  454. class cache_phpunit_dummy_overrideclass extends cache_application {
  455. // Satisfying the code pre-checker is just part of my day job.
  456. }
  457. /**
  458. * Cache PHPUnit specific factory.
  459. *
  460. * @copyright 2012 Sam Hemelryk
  461. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  462. */
  463. class cache_phpunit_factory extends cache_factory {
  464. /**
  465. * Exposes the cache_factory's disable method.
  466. *
  467. * Perhaps one day that method will be made public, for the time being it is protected.
  468. */
  469. public static function phpunit_disable() {
  470. parent::disable();
  471. }
  472. /**
  473. * Creates a store instance given its name and configuration.
  474. *
  475. * If the store has already been instantiated then the original object will be returned. (reused)
  476. *
  477. * @param string $name The name of the store (must be unique remember)
  478. * @param array $details
  479. * @param cache_definition $definition The definition to instantiate it for.
  480. * @return boolean|cache_store
  481. */
  482. public function create_store_from_config($name, array $details, cache_definition $definition) {
  483. if (isset($details['use_test_store'])) {
  484. // name, plugin, alt
  485. $class = 'cachestore_'.$details['plugin'];
  486. $method = 'initialise_unit_test_instance';
  487. if (class_exists($class) && method_exists($class, $method)) {
  488. $instance = $class::$method($definition);
  489. if ($instance) {
  490. return $instance;
  491. }
  492. }
  493. $details = $details['alt'];
  494. $name = $details['name'];
  495. }
  496. return parent::create_store_from_config($name, $details, $definition);
  497. }
  498. }