aboutsummaryrefslogtreecommitdiff
path: root/engine/tests/api/annotations.php
blob: c0b0687ccdf64f1380ab5e7686e1988b5d55bc97 (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
<?php
/**
 * Elgg Test annotation api
 *
 * @package Elgg
 * @subpackage Test
 */
class ElggCoreAnnotationAPITest extends ElggCoreUnitTest {
	protected $metastrings;

	/**
	 * Called before each test method.
	 */
	public function setUp() {
		$this->object = new ElggObject();
	}

	/**
	 * Called after each test method.
	 */
	public function tearDown() {
		// do not allow SimpleTest to interpret Elgg notices as exceptions
		$this->swallowErrors();

		unset($this->object);
	}

	public function testElggGetAnnotationsCount() {
		$this->object->title = 'Annotation Unit Test';
		$this->object->save();

		$guid = $this->object->getGUID();
		create_annotation($guid, 'tested', 'tested1', 'text', 0, ACCESS_PUBLIC);
		create_annotation($guid, 'tested', 'tested2', 'text', 0, ACCESS_PUBLIC);

		$count = (int)elgg_get_annotations(array(
			'annotation_names' => array('tested'),
			'guid' => $guid,
			'count' => true,
		));

		$this->assertIdentical($count, 2);

		$this->object->delete();
	}

	public function testElggDeleteAnnotations() {
		$e = new ElggObject();
		$e->save();

		for ($i=0; $i<30; $i++) {
			$e->annotate('test_annotation', rand(0,10000));
		}

		$options = array(
			'guid' => $e->getGUID(),
			'limit' => 0
		);

		$annotations = elgg_get_annotations($options);
		$this->assertIdentical(30, count($annotations));

		$this->assertTrue(elgg_delete_annotations($options));

		$annotations = elgg_get_annotations($options);
		$this->assertTrue(empty($annotations));

		// nothing to delete so null returned
		$this->assertNull(elgg_delete_annotations($options));

		$this->assertTrue($e->delete());
	}

	public function testElggDisableAnnotations() {
		$e = new ElggObject();
		$e->save();

		for ($i=0; $i<30; $i++) {
			$e->annotate('test_annotation', rand(0,10000));
		}

		$options = array(
			'guid' => $e->getGUID(),
			'limit' => 0
		);

		$this->assertTrue(elgg_disable_annotations($options));

		$annotations = elgg_get_annotations($options);
		$this->assertTrue(empty($annotations));

		access_show_hidden_entities(true);
		$annotations = elgg_get_annotations($options);
		$this->assertIdentical(30, count($annotations));
		access_show_hidden_entities(false);

		$this->assertTrue($e->delete());
	}

	public function testElggEnableAnnotations() {
		$e = new ElggObject();
		$e->save();

		for ($i=0; $i<30; $i++) {
			$e->annotate('test_annotation', rand(0,10000));
		}

		$options = array(
			'guid' => $e->getGUID(),
			'limit' => 0
		);

		$this->assertTrue(elgg_disable_annotations($options));

		// cannot see any annotations so returns null
		$this->assertNull(elgg_enable_annotations($options));

		access_show_hidden_entities(true);
		$this->assertTrue(elgg_enable_annotations($options));
		access_show_hidden_entities(false);

		$annotations = elgg_get_annotations($options);
		$this->assertIdentical(30, count($annotations));

		$this->assertTrue($e->delete());
	}

	public function testElggAnnotationExists() {
		$e = new ElggObject();
		$e->save();
		$guid = $e->getGUID();

		$this->assertFalse(elgg_annotation_exists($guid, 'test_annotation'));

		$e->annotate('test_annotation', rand(0, 10000));
		$this->assertTrue(elgg_annotation_exists($guid, 'test_annotation'));
		// this metastring should always exist but an annotation of this name should not
		$this->assertFalse(elgg_annotation_exists($guid, 'email'));

		$options = array(
			'guid' => $guid,
			'limit' => 0
		);
		$this->assertTrue(elgg_disable_annotations($options));
		$this->assertTrue(elgg_annotation_exists($guid, 'test_annotation'));

		$this->assertTrue($e->delete());
		$this->assertFalse(elgg_annotation_exists($guid, 'test_annotation'));
	}
}