Add og-test stuff.
[project/og.git] / tests / og_test.module
1 <?php
2
3 /**
4 * @file
5 * Test module for OG.
6 */
7
8 /**
9 * Implements hook_node_presave().
10 */
11 function og_test_node_presave($node) {
12 if (!empty($node->nid) && !empty($node->og_group_on_save)) {
13 $values = $node->og_group_on_save;
14 og_group($values['group_type'], $values['gid'], array('entity_type' => 'node', 'entity' => $node));
15 }
16 }
17
18 /**
19 * Implements hook_module_implements_alter().
20 */
21 function og_test_module_implements_alter(&$implementations, $hook) {
22 if ($hook != 'entity_delete') {
23 return;
24 }
25
26 // Switch the orders of the implementations.
27 $og = $implementations['og'];
28 $og_test = $implementations['og_test'];
29
30 unset($implementations['og'], $implementations['og_test']);
31
32 $implementations['og_test'] = $og_test;
33 $implementations['og'] = $og;
34 }
35
36 /**
37 * Implements hook_entity_delete().
38 */
39 function og_test_entity_delete($entity, $type) {
40 if (!og_is_group($type, $entity) || $entity->title != 'move' ) {
41 return;
42 }
43
44 // The children nodes of the deleted group need another group to be associated
45 // with. Using entity field query for getting the NID of another group.
46 $query = new EntityFieldQuery();
47 $result = $query
48 ->entityCondition('entity_type', 'node')
49 ->propertyCondition('nid', $entity->nid, '<>')
50 ->execute();
51
52 if (empty($result['node'])) {
53 return;
54 }
55
56 $nid = reset(array_keys($result['node']));
57
58 $entity->og_orphans = array(
59 'move' => array(
60 'group_type' => 'node',
61 'gid' => $nid,
62 ),
63 );
64 }
65
66 /**
67 * Implements hook_form_alter().
68 *
69 * @see OgBehaviorHandlerTestCase::testSetStateOnInsert()
70 */
71 function og_test_form_alter(&$form, $form_state) {
72 if (empty($form['#edit_node']) || $form['#bundle'] != 'behavior') {
73 return;
74 }
75
76 $form[OG_AUDIENCE_FIELD]['#element_validate'][] = 'og_test_form_behavior_validate';
77 }
78
79 /**
80 * Validate handler; Add state to the field values, if title is "state-pending".
81 . */
82 function og_test_form_behavior_validate($element, &$form_state) {
83 if ($form_state['values']['title'] != 'state-pending') {
84 return;
85 }
86
87 $form_state['values'][OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['state'] = OG_STATE_PENDING;
88 }