object_cache_unit.test 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Test ctools_cache_find_plugin and the structure of the default cache plugins.
  4. */
  5. class CtoolsUnitObjectCachePlugins extends DrupalWebTestCase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Object cache storage (unit tests)',
  12. 'description' => 'Verify that objects are written, readable and lockable.',
  13. 'group' => 'ctools',
  14. 'dependencies' => array('ctools'),
  15. );
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function setUp(array $modules = array()) {
  21. $modules[] = 'ctools';
  22. parent::setUp($modules);
  23. }
  24. /**
  25. * Check that the supplied array looks like a ctools cache plugin.
  26. *
  27. * @param mixed $p
  28. * The value to check.
  29. * @param string $msg
  30. * Prefix of the assertion message.
  31. */
  32. public function assertValidCachePlugin($p, $msg) {
  33. $this->assertTrue(is_array($p), $msg . ': plugin is an array');
  34. $this->assertEqual($p['plugin type'], 'cache', $msg . ': type is cache');
  35. $this->assertTrue(array_key_exists('title', $p), $msg . ': title element exists');
  36. $this->assertTrue(!empty($p['title']) && is_string($p['title']), $msg . ': title is a string');
  37. $this->assertTrue(array_key_exists('cache get', $p), $msg . ': has a get function');
  38. $this->assertTrue(!empty($p['cache get']) && is_callable($p['cache get']), $msg . ': get is executable');
  39. $this->assertTrue(array_key_exists('cache set', $p), $msg . ': has a set function');
  40. $this->assertTrue(!empty($p['cache set']) && is_callable($p['cache set']), $msg . ': set is executable');
  41. // @todo Clear is required by the spec (cache.inc:40..48): but export_ui
  42. // cache doesn't implement it. Enable the assertions when that problem is
  43. // solved.
  44. // $this->assertTrue(array_key_exists('cache clear', $p), $msg . ': has a clear function');
  45. // $this->assertTrue(is_callable($p['cache clear']), $msg . ': clear is executable');
  46. // @todo Break is optional acc'd to spec but does anything implement it?
  47. $this->assertTrue(!array_key_exists('cache break', $p) || is_callable($p['cache break']), $msg . ': break is executable');
  48. // @todo Finalize is optional so don't fail if not there??
  49. $this->assertTrue(!array_key_exists('cache finalize', $p) || is_callable($p['cache finalize']), $msg . ': finalize is executable');
  50. }
  51. /**
  52. * Check the return value of the ctools_cache_find_plugin function.
  53. *
  54. * @param mixed $p
  55. * The value to check.
  56. * @param string $msg
  57. * Prefix of the assertion message.
  58. */
  59. public function assertPluginNotFound($p, $msg) {
  60. $this->assertTrue(is_array($p), $msg . ': is an array');
  61. $plugin = array_shift($p);
  62. $this->assertNull($plugin, $msg . ': no plugin info');
  63. $data = array_shift($p);
  64. $this->assertTrue(empty($data) || is_string($data), $msg . ': data string-like');
  65. $this->assertTrue(empty($p), $msg . ': just two elements');
  66. }
  67. /**
  68. * Check the return value of the ctools_cache_find_plugin function.
  69. *
  70. * @param mixed $p
  71. * The value to check.
  72. * @param string $msg
  73. * Prefix of the assertion message.
  74. */
  75. public function assertPluginFound($p, $msg) {
  76. $this->assertTrue(is_array($p), $msg . ': is an array');
  77. $plugin = array_shift($p);
  78. $this->assertTrue(is_array($plugin), $msg . ': has plugin data');
  79. $data = array_shift($p);
  80. $this->assertTrue(empty($data) || is_string($data), $msg . ': data is string-like');
  81. $this->assertTrue(empty($p), $msg . ': just two elements');
  82. }
  83. /**
  84. * Test to see that we can find the standard simple plugin.
  85. */
  86. public function testFindSimpleCachePlugin() {
  87. ctools_include('cache');
  88. // The simple plugin.
  89. $plugin = ctools_cache_find_plugin('simple');
  90. $this->assertPluginFound($plugin, 'The Simple Cache plugin is present');
  91. $this->assertValidCachePlugin($plugin[0], 'The Simple Cache plugin');
  92. // The simple plugin, with ::.
  93. $plugin = ctools_cache_find_plugin('simple::data');
  94. $this->assertPluginFound($plugin, 'The Simple Cache plugin is present, with data');
  95. }
  96. /**
  97. * Test to see that we can find the standard export_ui plugin.
  98. */
  99. public function testFindExportUICachePlugin() {
  100. ctools_include('cache');
  101. // The export plugin.
  102. $plugin = ctools_cache_find_plugin('export_ui');
  103. $this->assertPluginFound($plugin, 'The Export UI Cache plugin is present');
  104. $this->assertValidCachePlugin($plugin[0], 'The Export Cache plugin');
  105. // The export plugin, with ::.
  106. $plugin = ctools_cache_find_plugin('export_ui::data');
  107. $this->assertTrue(is_array($plugin), 'The Export UI Cache plugin is present, with data');
  108. }
  109. /**
  110. * Test to see that we don't find plugins that aren't there.
  111. */
  112. public function testFindFoobarbazCachePlugin() {
  113. ctools_include('cache');
  114. // An imaginary foobarbaz plugin.
  115. $plugin = ctools_cache_find_plugin('foobarbaz');
  116. $this->assertPluginNotFound($plugin, 'The Foobarbaz Cache plugin is absent');
  117. $plugin = ctools_cache_find_plugin('foobarbaz::data');
  118. $this->assertPluginNotFound($plugin, 'The Foobarbaz Cache plugin is absent, with data');
  119. }
  120. }