aboutsummaryrefslogtreecommitdiff
path: root/engine/tests/api/metadata_cache.php
blob: 846116a7b3e958ff2d7631111f24beba44ddc778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
 * Elgg Test metadata cache
 *
 * @package Elgg
 * @subpackage Test
 */
class ElggCoreMetadataCacheTest extends ElggCoreUnitTest {

	/**
	 * @var ElggVolatileMetadataCache
	 */
	protected $cache;

	/**
	 * @var ElggObject
	 */
	protected $obj1;

	/**
	 * @var int
	 */
	protected $guid1;

	/**
	 * @var ElggObject
	 */
	protected $obj2;

	/**
	 * @var int
	 */
	protected $guid2;

	protected $name = 'test';
	protected $value = 'test';
	protected $ignoreAccess;

	/**
	 * Called before each test method.
	 */
	public function setUp() {
		$this->ignoreAccess = elgg_set_ignore_access(false);

		$this->cache = elgg_get_metadata_cache();

		$this->obj1 = new ElggObject();
		$this->obj1->save();
		$this->guid1 = $this->obj1->guid;

		$this->obj2 = new ElggObject();
		$this->obj2->save();
		$this->guid2 = $this->obj2->guid;
	}

	/**
	 * Called after each test method.
	 */
	public function tearDown() {
		$this->obj1->delete();
		$this->obj2->delete();

		elgg_set_ignore_access($this->ignoreAccess);
	}

	public function testBasicApi() {
		// test de-coupled instance
		$cache = new ElggVolatileMetadataCache();
		$cache->setIgnoreAccess(false);
		$guid = 1;

		$this->assertFalse($cache->isKnown($guid, $this->name));

		$cache->markEmpty($guid, $this->name);
		$this->assertTrue($cache->isKnown($guid, $this->name));
		$this->assertNull($cache->load($guid, $this->name));

		$cache->markUnknown($guid, $this->name);
		$this->assertFalse($cache->isKnown($guid, $this->name));

		$cache->save($guid, $this->name, $this->value);
		$this->assertIdentical($cache->load($guid, $this->name), $this->value);

		$cache->save($guid, $this->name, 1, true);
		$this->assertIdentical($cache->load($guid, $this->name), array($this->value, 1));

		$cache->clear($guid);
		$this->assertFalse($cache->isKnown($guid, $this->name));
	}

	public function testReadsAreCached() {
		// test that reads fill cache
		$this->obj1->setMetaData($this->name, $this->value);
		$this->cache->flush();

		$this->obj1->getMetaData($this->name);
		$this->assertIdentical($this->cache->load($this->guid1, $this->name), $this->value);
	}

	public function testWritesAreCached() {
		// delete should mark cache as known to be empty
		$this->obj1->deleteMetadata($this->name);
		$this->assertTrue($this->cache->isKnown($this->guid1, $this->name));
		$this->assertNull($this->cache->load($this->guid1, $this->name));

		// without name, delete should invalidate the entire entity
		$this->cache->save($this->guid1, $this->name, $this->value);
		elgg_delete_metadata(array(
			'guid' => $this->guid1,
		));
		$this->assertFalse($this->cache->isKnown($this->guid1, $this->name));

		// test set
		$this->obj1->setMetaData($this->name, $this->value);
		$this->assertIdentical($this->cache->load($this->guid1, $this->name), $this->value);

		// test set multiple
		$this->obj1->setMetaData($this->name, 1, 'integer', true);
		$this->assertIdentical($this->cache->load($this->guid1, $this->name), array($this->value, 1));

		// writes when access is ignore should invalidate
		$tmp_ignore = elgg_set_ignore_access(true);
		$this->obj1->setMetaData($this->name, $this->value);
		$this->assertFalse($this->cache->isKnown($this->guid1, $this->name));
		elgg_set_ignore_access($tmp_ignore);
	}

	public function testDisableAndEnable() {
		// both should mark cache unknown
		$this->obj1->setMetaData($this->name, $this->value);
		$this->obj1->disableMetadata($this->name);
		$this->assertFalse($this->cache->isKnown($this->guid1, $this->name));

		$this->cache->save($this->guid1, $this->name, $this->value);
		$this->obj1->enableMetadata($this->name);
		$this->assertFalse($this->cache->isKnown($this->guid1, $this->name));
	}

	public function testPopulateFromEntities() {
		// test populating cache from set of entities
		$this->obj1->setMetaData($this->name, $this->value);
		$this->obj1->setMetaData($this->name, 4, 'integer', true);
		$this->obj1->setMetaData("{$this->name}-2", "{$this->value}-2");
		$this->obj2->setMetaData($this->name, $this->value);

		$this->cache->flush();
		$this->cache->populateFromEntities(array($this->guid1, $this->guid2));

		$expected = array();
		$expected[$this->name][] = $this->value;
		$expected[$this->name][] = 4;
		$expected["{$this->name}-2"] = "{$this->value}-2";
		$this->assertIdentical($this->cache->loadAll($this->guid1), $expected);

		$expected = array();
		$expected[$this->name] = $this->value;
		$this->assertIdentical($this->cache->loadAll($this->guid2), $expected);
	}

	public function testFilterHeavyEntities() {
		$big_str = str_repeat('-', 5000);
		$this->obj2->setMetaData($this->name, array($big_str, $big_str));

		$guids = array($this->guid1, $this->guid2);
		$expected = array($this->guid1);
		$actual = $this->cache->filterMetadataHeavyEntities($guids, 6000);
		$this->assertIdentical($actual, $expected);
	}
}