aboutsummaryrefslogtreecommitdiff
path: root/engine/tests/objects/objects.php
blob: de3c2f1c4cc37efaac53756b8f5cc9fbf4562025 (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
<?php
/**
 * Elgg Test ElggObject
 *
 * @package Elgg
 * @subpackage Test
 * @author Curverider Ltd
 * @link http://elgg.org/
 */
class ElggCoreObjectTest extends ElggCoreUnitTest {

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

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

	/**
	 * Called after each test method.
	 */
	public function tearDown() {
		unset($this->entity);
	}

	/**
	 * Called after each test object.
	 */
	public function __destruct() {
		parent::__destruct();
	}

	/**
	 * A basic test that will be called and fail.
	 */
	public function testElggObjectConstructor() {
		$attributes = array();
		$attributes['guid'] = '';
		$attributes['type'] = 'object';
		$attributes['subtype'] = '';
		$attributes['owner_guid'] = get_loggedin_userid();
		$attributes['container_guid'] = get_loggedin_userid();
		$attributes['site_guid'] = 0;
		$attributes['access_id'] = ACCESS_PRIVATE;
		$attributes['time_created'] = '';
		$attributes['time_updated'] = '';
		$attributes['enabled'] = 'yes';
		$attributes['tables_split'] = 2;
		$attributes['tables_loaded'] = 0;
		$attributes['title'] = '';
		$attributes['description'] = '';

		$this->assertIdentical($this->entity->expose_attributes(), $attributes);
	}
	
	public function testElggObjectSave() {
		// new object
		$this->AssertEqual($this->entity->getGUID(), 0);
		$guid = $this->entity->save();
		$this->AssertNotEqual($guid, 0);
		
		$entity_row = $this->get_entity_row($guid);
		$this->assertIsA($entity_row, 'stdClass');
		
		// update existing object
		$this->entity->title = 'testing';
		$this->entity->description = 'ElggObject';
		$this->assertEqual($this->entity->save(), $guid);
		
		$object_row = $this->get_object_row($guid);
		$this->assertIsA($object_row, 'stdClass');
		$this->assertIdentical($object_row->title, 'testing');
		$this->assertIdentical($object_row->description, 'ElggObject');
		
		// clean up
		$this->entity->delete();
	}
	
	public function testElggObjectLoad() {
		// fail on wrong type
		try {
			$error = new ElggObjectTest(get_loggedin_userid());
			$this->assertTrue(FALSE);
		} catch (Exception $e) {
			$this->assertIsA($e, 'InvalidClassException');
			$message = sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), get_loggedin_userid(), 'ElggObject');
			$this->assertIdentical($e->getMessage(), $message);
		}
	}
	
	public function testElggObjectConstructorByGUID() {
		$guid = $this->entity->save();
		
		// load a new object using guid
		$entity = new ElggObjectTest($guid);
		$this->assertIdentical($this->entity, $entity);
		
		// clean up
		$this->entity->delete();
	}
	
	public function testElggObjectConstructorByObject() {
		$guid = $this->entity->save();
		
		// stdClass: use guid
		$object_row = $this->get_object_row($guid);
		$entity_row = $this->get_entity_row($guid);
		$this->assertIdentical($this->entity, new ElggObjectTest($object_row));
		$this->assertIdentical($this->entity, new ElggObjectTest($entity_row));
		
		// copy attributes of ElggObject
		$this->assertIdentical($this->entity, new ElggObjectTest($this->entity));
		
		// error on ElggEntity
		$entity = new ElggEntityTest($guid);
		try {
			$error = new ElggObjectTest($entity);
			$this->assertTrue(FALSE);
		} catch (Exception $e) {
			$this->assertIsA($e, 'InvalidParameterException');
			$this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:NonElggObject'));
		}
		
		// clean up
		$this->entity->delete();
	}
	
	public function testElggObjectContainer() {
		$this->assertEqual($this->entity->getContainer(), get_loggedin_userid());
		
		// fals when container not a group
		$this->assertFalse($this->entity->getContainerEntity());
		
		// create and save to group
		$group = new ElggGroup();
		$guid = $group->save();
		$this->assertTrue($this->entity->setContainer($guid));
		
		// check container
		$this->assertEqual($this->entity->getContainer(), $guid);
		$this->assertIdentical($group, $this->entity->getContainerEntity());
		
		// clean up
		$group->delete();
	}
	
	public function testElggObjectExportables() {
		$exportables = array(
			'guid',
			'type',
			'subtype',
			'time_created',
			'container_guid',
			'owner_guid',
			'title',
			'description'
		);
		
		$this->assertIdentical($exportables, $this->entity->getExportableValues());
	}
	
	
	protected function get_object_row($guid) {
		global $CONFIG;
		return get_data_row("SELECT * FROM {$CONFIG->dbprefix}objects_entity WHERE guid='$guid'");
	}
	
	protected function get_entity_row($guid) {
		global $CONFIG;
		return get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid='$guid'");
	}
}

class ElggObjectTest extends ElggObject {
	public function expose_attributes() {
		return $this->attributes;
	}
}