7 class Ninja_widgets_Test
extends PHPUnit_Framework_TestCase
{
8 public function setUp() {
9 Auth
::instance(array('session_key' => false))->force_user(new Op5User_AlwaysAuth());
10 $this->orig_widgets
= Ninja_widget_Model
::fetch_all('tac/index');
11 $this->assertTrue(is_array($this->orig_widgets
), "Fetch widgets returns an array");
12 $this->assertTrue(!empty($this->orig_widgets
), "Fetch widgets returns widgets");
13 foreach ($this->orig_widgets
as $w)
14 $this->assertTrue($w !== false, 'No returned widgets should be false');
15 $this->assertSame(Ninja_widget_Model
::get('tac/index', 'foobar', 3), false, "There shouldn't be a foobar widget with instance_id 3");
16 $this->assertSame(Ninja_widget_Model
::get('tac/index', 'foobar'), false, "There shouldn't be a foobar widget at all");
17 Ninja_widget_Model
::install('tac/index', 'foobar', 'Foo Bar');
19 public function tearDown() {
20 $db = Database
::instance();
21 foreach ($db->query('SELECT username, page, name, instance_id, count(1) as count FROM ninja_widgets GROUP BY username, page, name, instance_id') as $ary)
22 $this->assertEquals($ary->count
, 1, "There is 1 page=$ary->page,name=$ary->name,instance_id=$ary->instance_id,username=$ary->username");
23 $this->assertTrue(Ninja_widget_Model
::uninstall('foobar'), "Widget was uninstalled");
24 $new_widgets = Ninja_widget_Model
::fetch_all('tac/index');
25 foreach ($new_widgets as $w)
26 $this->assertTrue($w !== false, 'No returned widgets should be false');
27 $this->assertTrue(is_array($new_widgets), "Fetch widgets returns an array");
28 $this->assertTrue(!empty($new_widgets), "Fetch widgets returns widgets");
29 $this->assertTrue(count($new_widgets) === count($this->orig_widgets
), 'The new widget is gone (are '.count($new_widgets).', was '.count($this->orig_widgets
).')');
30 foreach ($db->query('SELECT username, page, name, instance_id, count(1) as count FROM ninja_widgets GROUP BY username, page, name, instance_id') as $ary)
31 $this->assertEquals($ary->count
, 1, "There is 1 page=$ary->page,name=$ary->name,instance_id=$ary->instance_id,username=$ary->username");
34 public function test_table_ninja_widgets_table_exists()
36 $db = Database
::instance();
37 $table = 'ninja_widgets';
38 $this->assertTrue($db->table_exists($table), "Unable to find table $table");
42 * check that we have default database settings for all widgets
45 public function test_widgets_db_settings()
48 'error', // Internal implementation detail, should never be visible in widget listings
49 'geomap', // In ninja, not in monitor - it's relationship status is It's Complicated
50 'tac_services_common', // Implementation detail for tac_services_critical_(un)?acknowledged widgets
51 'nagvis', // Installs through post-hook in spec file... Needs to be installed on build server...
52 'listview' // Isn't fetched through database, but constant settings when called. (Might change later)
54 $db = Database
::instance();
55 $table = 'ninja_widgets';
56 $widget_list = array();
59 # finnd all widgets and put in widget_list array
60 if ($handle = opendir(APPPATH
.'widgets/')) {
61 while (false !== ($file = readdir($handle))) {
62 if (is_dir(APPPATH
.'widgets/'.$file) && $file != "." && $file != "..") {
63 $widget_list[] = $file;
70 # check custom_widgets and assign to widget_list if not already done
71 if ($handle = opendir(APPPATH
.'custom_widgets/')) {
72 while (false !== ($file = readdir($handle))) {
73 if (is_dir(APPPATH
.'widgets/'.$file) && $file != "." && $file != ".." && !in_array($file, $widget_list)) {
74 $widget_list[] = $file;
81 foreach ($widget_list as $widget) {
82 $sql = "SELECT * FROM ".$table." WHERE username IS NULL AND name = ".$db->escape($widget);
83 $result = $db->query($sql);
84 if (!count($result) && !in_array($widget, $excluded))
88 $this->assertTrue(empty($missing), 'Missing database settings for '.implode(',',$missing));
91 function test_get_widget() {
92 $widget = Ninja_widget_Model
::get('tac/index', 'foobar');
93 $this->assertTrue($widget !== false, 'Create new widget is successful');
94 $this->assertSame(Ninja_widget_Model
::get('tac/index', 'foobar', 3), false, "There still isn't a foobar widget with instance_id 3");
95 $this->assertSame($widget->page
, 'tac/index', 'New page has the given page name');
96 $new_widgets = Ninja_widget_Model
::fetch_all('tac/index');
97 foreach ($new_widgets as $w)
98 $this->assertTrue($w !== false, 'No returned widgets should be false');
99 $this->assertTrue(is_array($new_widgets), "Fetch widgets returns an array");
100 $this->assertTrue(!empty($new_widgets), "Fetch widgets isn't empty");
101 $this->assertTrue(count($new_widgets) === count($this->orig_widgets
) +
1, 'There should be 1 new widget, there is ' +
count($new_widgets) - count($this->orig_widgets
));
104 function test_copy_widget() {
105 $widget = Ninja_widget_Model
::get('tac/index', 'foobar');
106 $widget = $widget->copy();
107 $this->assertTrue($widget !== false, 'Create new widget is successful');
108 $dup_widget = $widget->copy();
109 $this->assertTrue($dup_widget !== false, 'Copy widget is successful');
110 $this->assertTrue($dup_widget->instance_id
== $widget->instance_id +
1, 'New widget has bumped instance id');
111 $new_widgets = Ninja_widget_Model
::fetch_all('tac/index');
112 $this->assertTrue(count($new_widgets) === count($this->orig_widgets
) +
2, 'Both copies are fetched on fetch');
113 $dup2 = $dup_widget->copy();
114 $this->assertTrue($dup2 !== false, 'Copy copied widget is successful');
115 $this->assertTrue($dup2->instance_id
== $dup_widget->instance_id +
1, 'New widget has bumped instance id');
116 $dup3 = $widget->copy();
117 $this->assertTrue($dup3 !== false, 'Copy original widget without highest instance_id is successful');
118 $this->assertTrue($dup3->instance_id
== $dup2->instance_id +
1, 'New widget has bumped instance id');
121 $dup_widget->delete();
122 $new_widgets = Ninja_widget_Model
::fetch_all('tac/index');
123 $this->assertTrue(count($new_widgets) === count($this->orig_widgets
) +
1, 'After deleting copied widgets, only the original remains');
125 $new_widgets = Ninja_widget_Model
::fetch_all('tac/index');
126 $this->assertTrue(count($new_widgets) === count($this->orig_widgets
) +
1, "Last widget copy can't be deleted");
129 /** there were no instance_ids in the past */
130 function test_edit_legacy() {
131 $username = Auth
::instance()->get_user()->username
;
132 $db = Database
::instance();
133 $db->query("INSERT INTO ninja_widgets (name, username, page, instance_id) VALUES ('foobar2', '$username', 'tac/index', null)");
134 $widgets = Ninja_widget_Model
::fetch_all('tac/index');
136 foreach ($widgets as $widget) {
137 if ($widget->name
=== 'foobar2') {
142 $this->assertTrue($foobar !== false, 'Foobar2 widget found');
143 $this->assertSame($foobar->instance_id
, NULL, 'Instance ID is initially NULL');
145 $this->assertSame($foobar->instance_id
, 1, 'Instance ID is set to a number after save');
146 $foobar->friendly_name
='w00t';
148 $res = $db->query("SELECT count(1) AS count FROM ninja_widgets WHERE name='foobar2'");
149 $this->assertEquals($res->current()->count
, 2, 'There should be 2 foobar2 widgets in DB after write, there are '.$res->current()->count
);
150 $this->assertTrue(Ninja_widget_Model
::uninstall('foobar2'), "Widget can be uninstalled");
153 function test_settings() {
154 $widget = Ninja_widget_Model
::get('tac/index', 'foobar');
155 $widget->merge_settings(array('foo' => 'bar', 'baz' => 3));
156 $dupe = $widget->copy();
158 $saved_widget = Ninja_widget_Model
::get('tac/index', 'foobar', $widget->instance_id
);
159 $this->assertTrue(!empty($saved_widget->setting
), 'There are settings saved');
160 $this->assertSame($widget->setting
, $saved_widget->setting
, 'The settings are identical');
161 $saved_dupe = Ninja_widget_Model
::get('tac/index', 'foobar', $dupe->instance_id
);
162 $this->assertEmpty($saved_dupe->setting
, 'There are no settings propagated to dupes');
165 function test_widget_helper() {
166 $widget = Ninja_widget_Model
::get('tac/index', 'foobar');
168 $ws = Ninja_widget_Model
::fetch_all('tac/index');
169 foreach ($ws as $name => $widget_obj) {
170 $this->assertSame($name, 'widget-'.$widget_obj->name
.'-'.$widget_obj->instance_id
, 'All widgets have correct array indexes');
172 $order = Ninja_widget_Model
::fetch_widget_order('tac/index');
173 $order['test_placeholder'] = array('widget-foobar-1');
174 $this->assertTrue(Ninja_widget_Model
::set_widget_order('tac/index', $order), "set widget order is successful");
175 $widgets = widget
::add_widgets('tac/index', $ws, $this);
176 $this->assertTrue(isset($widgets['test_placeholder']), 'The new placeholder exists');
177 $this->assertTrue(isset($widgets['test_placeholder']['widget-foobar-1']), 'Our widget is saved in our placeholder');
178 $foobar = Ninja_widget_Model
::get('tac/index', 'foobar', 1);
179 $foobar2 = $foobar->copy();
180 $ws = Ninja_widget_Model
::fetch_all('tac/index');
181 $widgets = widget
::add_widgets('tac/index', $ws, $this);
182 $this->assertTrue(isset($widgets['test_placeholder']), 'The new placeholder exists');
183 $this->assertTrue(isset($widgets['test_placeholder']['widget-foobar-1']), 'Our old widget is saved in our placeholder');
184 $this->assertTrue(isset($widgets['test_placeholder']['widget-foobar-2']), 'Our new widget is saved in the same placeholder as it\'s original');
187 function test_many_users() {
188 $widget = Ninja_widget_Model
::get('tac/index', 'foobar');
189 $a = Auth
::instance()->get_user();
190 $real_username = $a->username
;
191 $a->username
= 'something else';
192 Ninja_widget_Model
::fetch_all('tac/index');
193 $a->username
= $real_username;