aboutsummaryrefslogtreecommitdiff
path: root/engine/tests/api/metastrings.php
blob: 0a8945084faab682dd71618d398ed8e6d7f94649 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
 * Elgg Metastrings test
 *
 * @package Elgg.Core
 * @subpackage Metastrings.Test
 */
class ElggCoreMetastringsTest extends ElggCoreUnitTest {

	public $metastringTypes = array('metadata', 'annotations');

	/**
	 * Called before each test object.
	 */
	public function __construct() {
		parent::__construct();

		$this->metastrings = array();
		$this->object = new ElggObject();
		$this->object->save();
	}

	public function createAnnotations($max = 1) {
		$annotations = array();
		for ($i=0; $i<$max; $i++) {
			$name = 'test_annotation_name' . rand();
			$value = 'test_annotation_value' . rand();
			$id = create_annotation($this->object->guid, $name, $value);
			$annotations[] = $id;
		}

		return $annotations;
	}

	public function createMetadata($max = 1) {
		$metadata = array();
		for ($i=0; $i<$max; $i++) {
			$name = 'test_metadata_name' . rand();
			$value = 'test_metadata_value' . rand();
			$id = create_metadata($this->object->guid, $name, $value);
			$metadata[] = $id;
		}

		return $metadata;
	}

	/**
	 * Called before each test method.
	 */
	public function setUp() {

	}

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

	/**
	 * Called after each test object.
	 */
	public function __destruct() {
		$this->object->delete();

		parent::__destruct();
	}

	public function testDeleteByID() {
		$db_prefix = elgg_get_config('dbprefix');
		$annotations = $this->createAnnotations(1);
		$metadata = $this->createMetadata(1);

		foreach ($this->metastringTypes as $type) {
			$id = ${$type}[0];
			$table = $db_prefix . $type;
			$q = "SELECT * FROM $table WHERE id = $id";
			$test = get_data($q);

			$this->assertEqual($test[0]->id, $id);
			$this->assertIdentical(true, elgg_delete_metastring_based_object_by_id($id, $type));
			$this->assertIdentical(array(), get_data($q));
		}
	}

	public function testGetMetastringObjectFromID() {
		$db_prefix = elgg_get_config('dbprefix');
		$annotations = $this->createAnnotations(1);
		$metadata = $this->createMetadata(1);

		foreach ($this->metastringTypes as $type) {
			$id = ${$type}[0];
			$test = elgg_get_metastring_based_object_from_id($id, $type);

			$this->assertEqual($id, $test->id);
		}
	}

	public function testEnableDisableByID() {
		$db_prefix = elgg_get_config('dbprefix');
		$annotations = $this->createAnnotations(1);
		$metadata = $this->createMetadata(1);

		foreach ($this->metastringTypes as $type) {
			$id = ${$type}[0];
			$table = $db_prefix . $type;
			$q = "SELECT * FROM $table WHERE id = $id";
			$test = get_data($q);

			// disable
			$this->assertEqual($test[0]->enabled, 'yes');
			$this->assertTrue(elgg_set_metastring_based_object_enabled_by_id($id, 'no', $type));

			$test = get_data($q);
			$this->assertEqual($test[0]->enabled, 'no');

			// enable
			$ashe = access_get_show_hidden_status();
			access_show_hidden_entities(true);
			flush();
			$this->assertTrue(elgg_set_metastring_based_object_enabled_by_id($id, 'yes', $type));

			$test = get_data($q);
			$this->assertEqual($test[0]->enabled, 'yes');

			access_show_hidden_entities($ashe);
		}
	}

	public function testKeepMeFromDeletingEverything() {
		foreach ($this->metastringTypes as $type) {
			$required = array(
				'guid', 'guids'
			);

			switch ($type) {
				case 'metadata':
					$metadata_required = array(
						'metadata_owner_guid', 'metadata_owner_guids',
						'metadata_name', 'metadata_names',
						'metadata_value', 'metadata_values'
					);

					$required = array_merge($required, $metadata_required);
					break;

				case 'annotations':
					$annotations_required = array(
						'annotation_owner_guid', 'annotation_owner_guids',
						'annotation_name', 'annotation_names',
						'annotation_value', 'annotation_values'
					);

					$required = array_merge($required, $annotations_required);
					break;
			}

			$options = array();
			$this->assertFalse(elgg_is_valid_options_for_batch_operation($options, $type));

			// limit alone isn't valid:
			$options = array('limit' => 10);
			$this->assertFalse(elgg_is_valid_options_for_batch_operation($options, $type));

			foreach ($required as $key) {
				$options = array();

				$options[$key] = ELGG_ENTITIES_ANY_VALUE;
				$this->assertFalse(elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = ELGG_ENTITIES_ANY_VALUE");

				$options[$key] = ELGG_ENTITIES_NO_VALUE;
				$this->assertFalse(elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = ELGG_ENTITIES_NO_VALUE");

				$options[$key] = false;
				$this->assertFalse(elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = bool false");

				$options[$key] = true;
				$this->assertTrue(elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = bool true");

				$options[$key] = 'test';
				$this->assertTrue(elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = 'test'");

				$options[$key] = array('test');
				$this->assertTrue(elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = array('test')");
			}
		}
	}
}