Test Failed
Push — CI ( 0f01dd...c95a04 )
by Adam
55:13
created

logic_utilsTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 114
rs 10
c 1
b 0
f 1
1
<?php
2
3
use org\bovigo\vfs\vfsStream,
4
org\bovigo\vfs\vfsStreamDirectory;
5
6
require_once 'include/utils/logic_utils.php';
7
8
class logic_utilsTest extends PHPUnit_Framework_TestCase
9
{
10
11
	public function testget_hook_array()
12
	{
13
		//test with a vaid module. it will return an array
14
		$AccountsHooks =  get_hook_array('Accounts');
15
		$this->assertTrue(is_array($AccountsHooks));
16
		
17
		//test with an invalid array. it will throw an file include exception.
18
		$BugsHooks = "";
19
		try{
20
			$BugsHooks =  get_hook_array('Bugs');
21
		}catch(Exception $e){}
22
		
23
		$this->assertFalse(is_array($BugsHooks));
24
		
25
	}
26
	
27
	
28
	private function getTestHook(){
29
		
30
		//array containing hooks array to test
31
		
32
        $hook_array = Array();
33
        $hook_array['after_ui_footer'] = Array();
34
        $hook_array['after_ui_footer'][] = Array(10, 'popup_onload', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_onload');
35
        $hook_array['after_ui_frame'] = Array();
36
        $hook_array['after_ui_frame'][] = Array(20, 'mass_assign', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'mass_assign');
37
        $hook_array['after_ui_frame'][] = Array(1, 'Load Social JS', 'custom/include/social/hooks.php','hooks', 'load_js');
38
        $hook_array['after_save'] = Array();
39
        $hook_array['after_save'][] = Array(30, 'popup_select', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_select');
40
        $hook_array['after_save'][] = Array(1, 'AOD Index Changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleChanges');
41
        $hook_array['after_delete'] = array();
42
        $hook_array['after_delete'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleDelete');
43
        $hook_array['after_restore'] = array();
44
        $hook_array['after_restore'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleRestore');
45
        
46
		return $hook_array;
47
	}
48
	
49
	public function check_existing_elementProvider()
50
	{
51
		//provide test cases dataset to validate 
52
		
53
		$hook_array = $this->getTestHook();
54
		return array(
55
				array($hook_array,'after_save',array(0,'popup_select'), true),
56
				array($hook_array,'after_save',array(0,'foo'), false),
57
				array($hook_array,'foo',array(0,'popup_select'), false),
58
		);	
59
	}
60
	
61
	/**
62
	 * @dataProvider check_existing_elementProvider
63
	 */
64
	public function testcheck_existing_element($hook_array, $event, $action_array, $expected)
65
	{
66
		//test with dataset containing valid and invalid cases
67
		$this->assertSame( check_existing_element($hook_array,$event, $action_array) , $expected);
68
		
69
	}
70
	
71
	public function testreplace_or_add_logic_type()
72
	{
73
		//execute the method and test if it returns expected values
74
		
75
		$hook_array = $this->getTestHook();
76
		$expected = "<?php\n// Do not store anything in this file that is not part of the array or the hook version.  This file will	\n// be automatically rebuilt in the future. \n \$hook_version = 1; \n\$hook_array = Array(); \n// position, file, function \n\$hook_array['after_ui_footer'] = Array(); \n\$hook_array['after_ui_footer'][] = Array(10, 'popup_onload', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_onload'); \n\$hook_array['after_ui_frame'] = Array(); \n\$hook_array['after_ui_frame'][] = Array(20, 'mass_assign', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'mass_assign'); \n\$hook_array['after_ui_frame'][] = Array(1, 'Load Social JS', 'custom/include/social/hooks.php','hooks', 'load_js'); \n\$hook_array['after_save'] = Array(); \n\$hook_array['after_save'][] = Array(30, 'popup_select', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_select'); \n\$hook_array['after_save'][] = Array(1, 'AOD Index Changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleChanges'); \n\$hook_array['after_delete'] = Array(); \n\$hook_array['after_delete'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleDelete'); \n\$hook_array['after_restore'] = Array(); \n\$hook_array['after_restore'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleRestore'); \n\n\n\n?>";
77
		$this->assertEquals($expected,replace_or_add_logic_type($hook_array));
78
		
79
	}
80
	
81
	public function testwrite_logic_file()
82
	{
83
		//execute the method and test if it returns expected values, 
84
		//check if file is created and contains expected contents
85
	
86
		$vfs = vfsStream::setup('custom/modules/TEST_Test');
87
	
88
		if ( $vfs->hasChild('logic_hooks.php')  == true) {
89
			unlink('custom/modules/TEST_Test/logic_hooks.php');
90
			rmdir('custom/modules/TEST_Test');
91
		}
92
		
93
		$expectedContents = "<?php\n// Do not store anything in this file that is not part of the array or the hook version.  This file will	\n// be automatically rebuilt in the future. \n \$hook_version = 1; \n\$hook_array = Array(); \n// position, file, function \n\$hook_array['after_ui_footer'] = Array(); \n\$hook_array['after_ui_footer'][] = Array(10, 'popup_onload', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_onload'); \n\$hook_array['after_ui_frame'] = Array(); \n\$hook_array['after_ui_frame'][] = Array(20, 'mass_assign', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'mass_assign'); \n\$hook_array['after_ui_frame'][] = Array(1, 'Load Social JS', 'custom/include/social/hooks.php','hooks', 'load_js'); \n\$hook_array['after_save'] = Array(); \n\$hook_array['after_save'][] = Array(30, 'popup_select', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_select'); \n\$hook_array['after_save'][] = Array(1, 'AOD Index Changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleChanges'); \n\$hook_array['after_delete'] = Array(); \n\$hook_array['after_delete'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleDelete'); \n\$hook_array['after_restore'] = Array(); \n\$hook_array['after_restore'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleRestore'); \n\n\n\n?>";
94
		write_logic_file('TEST_Test',$expectedContents);
95
		
96
		//Check file created
97
		$this->assertFileExists('custom/modules/TEST_Test/logic_hooks.php');
98
		$actualContents = file_get_contents('custom/modules/TEST_Test/logic_hooks.php');
99
		$this->assertSame($expectedContents, $actualContents);
100
		
101
		$expectedArray = $this->getTestHook();
102
		
103
		$actualArray = get_hook_array('TEST_Test');
104
		
105
		$this->assertSame($expectedArray, $actualArray);
106
		
107
		unlink('custom/modules/TEST_Test/logic_hooks.php');
108
		rmdir('custom/modules/TEST_Test');		
109
		
110
	}
111
	
112
	public function testbuild_logic_file()
113
	{
114
		//execute the method and test if it returns expected values
115
		
116
		$hook_array = $this->getTestHook();
117
		$expected = "// Do not store anything in this file that is not part of the array or the hook version.  This file will	\n// be automatically rebuilt in the future. \n \$hook_version = 1; \n\$hook_array = Array(); \n// position, file, function \n\$hook_array['after_ui_footer'] = Array(); \n\$hook_array['after_ui_footer'][] = Array(10, 'popup_onload', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_onload'); \n\$hook_array['after_ui_frame'] = Array(); \n\$hook_array['after_ui_frame'][] = Array(20, 'mass_assign', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'mass_assign'); \n\$hook_array['after_ui_frame'][] = Array(1, 'Load Social JS', 'custom/include/social/hooks.php','hooks', 'load_js'); \n\$hook_array['after_save'] = Array(); \n\$hook_array['after_save'][] = Array(30, 'popup_select', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_select'); \n\$hook_array['after_save'][] = Array(1, 'AOD Index Changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleChanges'); \n\$hook_array['after_delete'] = Array(); \n\$hook_array['after_delete'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleDelete'); \n\$hook_array['after_restore'] = Array(); \n\$hook_array['after_restore'][] = Array(1, 'AOD Index changes', 'modules/AOD_Index/AOD_LogicHooks.php','AOD_LogicHooks', 'saveModuleRestore'); \n\n\n";
118
		$this->assertEquals($expected,build_logic_file($hook_array));	
119
	}
120
	
121
}
122
?>
123