ElggCoreMetadataCacheTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. use Elgg\Cache\MetadataCache;
  3. /**
  4. * Elgg Test metadata cache
  5. *
  6. * @package Elgg
  7. * @subpackage Test
  8. */
  9. class ElggCoreMetadataCacheTest extends \ElggCoreUnitTest {
  10. /**
  11. * @var MetadataCache
  12. */
  13. protected $cache;
  14. /**
  15. * @var \ElggObject
  16. */
  17. protected $obj1;
  18. /**
  19. * @var int
  20. */
  21. protected $guid1;
  22. /**
  23. * @var \ElggObject
  24. */
  25. protected $obj2;
  26. /**
  27. * @var int
  28. */
  29. protected $guid2;
  30. protected $name = 'test';
  31. protected $value = 'test';
  32. protected $ignoreAccess;
  33. /**
  34. * Called before each test method.
  35. */
  36. public function setUp() {
  37. $this->ignoreAccess = elgg_set_ignore_access(false);
  38. $this->cache = _elgg_services()->metadataCache;
  39. $this->obj1 = new \ElggObject();
  40. $this->obj1->save();
  41. $this->guid1 = $this->obj1->guid;
  42. $this->obj2 = new \ElggObject();
  43. $this->obj2->save();
  44. $this->guid2 = $this->obj2->guid;
  45. }
  46. /**
  47. * Called after each test method.
  48. */
  49. public function tearDown() {
  50. $this->obj1->delete();
  51. $this->obj2->delete();
  52. elgg_set_ignore_access($this->ignoreAccess);
  53. }
  54. public function testHas() {
  55. $cache = new MetadataCache(ElggSession::getMock());
  56. $cache->inject(1, ['foo1' => 'bar']);
  57. $cache->inject(2, []);
  58. $this->assertTrue($cache->isLoaded(1));
  59. $this->assertTrue($cache->isLoaded(2));
  60. $this->assertFalse($cache->isLoaded(3));
  61. }
  62. public function testLoad() {
  63. $cache = new MetadataCache(ElggSession::getMock());
  64. $cache->inject(1, ['foo1' => 'bar']);
  65. $cache->inject(2, []);
  66. $this->assertIdentical($cache->getSingle(1, 'foo1'), 'bar');
  67. $this->assertNull($cache->getSingle(1, 'foo2'));
  68. $this->assertNull($cache->getSingle(2, 'foo1'));
  69. }
  70. public function testDirectInvalidation() {
  71. $cache = new MetadataCache(ElggSession::getMock());
  72. $cache->inject(1, ['foo1' => 'bar']);
  73. $cache->inject(2, []);
  74. $cache->invalidateByOptions(['guid' => 1]);
  75. $this->assertFalse($cache->isLoaded(1));
  76. $this->assertTrue($cache->isLoaded(2));
  77. $cache->invalidateByOptions([]);
  78. $this->assertFalse($cache->isLoaded(1));
  79. $this->assertFalse($cache->isLoaded(2));
  80. }
  81. public function testCacheIsSegregatedByAccessState() {
  82. $session = ElggSession::getMock();
  83. $cache = new MetadataCache($session);
  84. $cache->inject(1, ['foo' => 'bar']);
  85. $session->setIgnoreAccess();
  86. $this->assertFalse($cache->isLoaded(1));
  87. $session->setIgnoreAccess(false);
  88. $this->assertTrue($cache->isLoaded(1));
  89. $user = elgg_get_entities(['type' => 'user', 'limit' => 1]);
  90. $user = $user[0];
  91. $cache->inject(1, ['foo' => 'bar']);
  92. $session->setLoggedInUser($user);
  93. $this->assertFalse($cache->isLoaded(1));
  94. }
  95. public function testClearActsOnAllAccessStates() {
  96. $session = ElggSession::getMock();
  97. $cache = new MetadataCache($session);
  98. $session->setIgnoreAccess(false);
  99. $cache->inject(1, ['foo' => 'bar']);
  100. $session->setIgnoreAccess(true);
  101. $cache->clear(1);
  102. $session->setIgnoreAccess(false);
  103. $this->assertFalse($cache->isLoaded(1));
  104. $session->setIgnoreAccess(true);
  105. $cache->inject(1, ['foo' => 'bar']);
  106. $session->setIgnoreAccess(false);
  107. $cache->clear(1);
  108. $session->setIgnoreAccess(true);
  109. $this->assertFalse($cache->isLoaded(1));
  110. }
  111. public function testMetadataReadsFillsCache() {
  112. // test that reads fill cache
  113. $this->obj1->setMetadata($this->name, [1, 2]);
  114. $this->cache->clearAll();
  115. $this->obj1->getMetadata($this->name);
  116. $this->assertIdentical($this->cache->getSingle($this->guid1, $this->name), [1, 2]);
  117. }
  118. public function testWritesInvalidate() {
  119. // elgg_delete_metadata
  120. $this->cache->inject($this->guid1, ['foo' => 'bar']);
  121. $this->cache->inject($this->guid2, ['bing' => 'bar']);
  122. elgg_delete_metadata(array(
  123. 'guid' => $this->guid1,
  124. ));
  125. $this->assertFalse($this->cache->isLoaded($this->guid1));
  126. $this->assertTrue($this->cache->isLoaded($this->guid2));
  127. $this->cache->inject($this->guid1, ['foo' => 'bar']);
  128. $this->cache->inject($this->guid2, ['bing' => 'bar']);
  129. elgg_delete_metadata(['guids' => [$this->guid1, $this->guid2]]);
  130. $this->assertFalse($this->cache->isLoaded($this->guid1));
  131. $this->assertFalse($this->cache->isLoaded($this->guid2));
  132. // setMetadata
  133. $this->cache->inject($this->guid1, ['foo' => 'bar']);
  134. $this->obj1->setMetadata($this->name, $this->value);
  135. $this->assertFalse($this->cache->isLoaded($this->obj1));
  136. // deleteMetadata
  137. $this->cache->inject($this->guid1, ['foo' => 'bar']);
  138. $this->obj1->deleteMetadata($this->name);
  139. $this->assertFalse($this->cache->isLoaded($this->guid1));
  140. // create_metadata
  141. $this->cache->inject($this->guid1, ['foo' => 'bar']);
  142. create_metadata($this->guid1, 'foo', 'bar', 'text');
  143. $this->assertFalse($this->cache->isLoaded($this->guid1));
  144. // disableMetadata
  145. $this->obj1->setMetadata($this->name, $this->value);
  146. $this->cache->inject($this->guid1, ['foo' => 'bar']);
  147. $this->obj1->disableMetadata($this->name);
  148. $this->assertFalse($this->cache->isLoaded($this->guid1));
  149. // enableMetadata
  150. $this->cache->inject($this->guid1, ['foo' => 'bar']);
  151. $this->obj1->enableMetadata($this->name);
  152. $this->assertFalse($this->cache->isLoaded($this->guid1));
  153. }
  154. public function testPopulateFromEntities() {
  155. // test populating cache from set of entities
  156. $this->obj1->setMetadata($this->name, $this->value);
  157. $this->obj1->setMetadata($this->name, 4, 'integer', true);
  158. $this->obj1->setMetadata("{$this->name}-2", "{$this->value}-2");
  159. $this->obj2->setMetadata($this->name, $this->value);
  160. $this->cache->clearAll();
  161. $this->cache->populateFromEntities(array($this->guid1, $this->guid2));
  162. $this->assertIdentical($this->cache->getSingle($this->guid1, $this->name), [
  163. $this->value,
  164. 4,
  165. ]);
  166. $this->assertIdentical($this->cache->getSingle($this->guid1, "{$this->name}-2"), "{$this->value}-2");
  167. $this->assertIdentical($this->cache->getSingle($this->guid2, $this->name), $this->value);
  168. }
  169. public function testFilterHeavyEntities() {
  170. $big_str = str_repeat('-', 5000);
  171. $this->obj2->setMetadata($this->name, array($big_str, $big_str));
  172. $guids = array($this->guid1, $this->guid2);
  173. $expected = array($this->guid1);
  174. $actual = $this->cache->filterMetadataHeavyEntities($guids, 6000);
  175. $this->assertIdentical($actual, $expected);
  176. }
  177. }