2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PMA_Message class
6 * @package phpMyAdmin-test
12 require_once 'libraries/sanitizing.lib.php';
13 require_once 'libraries/core.lib.php';
14 require_once 'libraries/Message.class.php';
16 class PMA_Message_test
extends PHPUnit_Extensions_OutputTestCase
25 * Sets up the fixture, for example, opens a network connection.
26 * This method is called before a test is executed.
30 protected function setUp()
32 $this->object = new PMA_Message
;
36 * Tears down the fixture, for example, closes a network connection.
37 * This method is called after a test is executed.
41 protected function tearDown()
46 * to String casting test
48 public function test__toString()
50 $this->object->setMessage('test<&>', true);
51 $this->assertEquals('test<&>', (string)$this->object);
57 public function testSuccess()
59 $this->object = new PMA_Message('test<&>', PMA_Message
::SUCCESS
);
60 $this->assertEquals($this->object, PMA_Message
::success('test<&>'));
61 $this->assertEquals('Your SQL query has been executed successfully', PMA_Message
::success()->getString());
67 public function testError()
69 $this->object = new PMA_Message('test<&>', PMA_Message
::ERROR
);
70 $this->assertEquals($this->object, PMA_Message
::error('test<&>'));
71 $this->assertEquals('Error', PMA_Message
::error()->getString());
77 public function testNotice()
79 $this->object = new PMA_Message('test<&>', PMA_Message
::NOTICE
);
80 $this->assertEquals($this->object, PMA_Message
::notice('test<&>'));
84 * test rawError method
86 public function testRawError()
88 $this->object = new PMA_Message('', PMA_Message
::ERROR
);
89 $this->object->setMessage('test<&>');
91 $this->assertEquals($this->object, PMA_Message
::rawError('test<&>'));
95 * test rawNotice method
97 public function testRawNotice()
99 $this->object = new PMA_Message('', PMA_Message
::NOTICE
);
100 $this->object->setMessage('test<&>');
102 $this->assertEquals($this->object, PMA_Message
::rawNotice('test<&>'));
106 * test rawSuccess method
108 public function testRawSuccess()
110 $this->object = new PMA_Message('', PMA_Message
::SUCCESS
);
111 $this->object->setMessage('test<&>');
113 $this->assertEquals($this->object, PMA_Message
::rawSuccess('test<&>'));
117 * testing isSuccess method
119 public function testIsSuccess()
121 $this->assertFalse($this->object->isSuccess());
122 $this->assertTrue($this->object->isSuccess(true));
126 * testing isNotice method
128 public function testIsNotice()
130 $this->assertTrue($this->object->isNotice());
131 $this->object->isError(true);
132 $this->assertFalse($this->object->isNotice());
133 $this->assertTrue($this->object->isNotice(true));
137 * testing isError method
139 public function testIsError()
141 $this->assertFalse($this->object->isError());
142 $this->assertTrue($this->object->isError(true));
146 * testign setter of message
148 public function testSetMessage()
150 $this->object->setMessage('test&<>', false);
151 $this->assertEquals('test&<>', $this->object->getMessage());
152 $this->object->setMessage('test&<>', true);
153 $this->assertEquals('test&<>', $this->object->getMessage());
157 * testing setter of string
159 public function testSetString()
161 $this->object->setString('test&<>', false);
162 $this->assertEquals('test&<>', $this->object->getString());
163 $this->object->setString('test&<>', true);
164 $this->assertEquals('test&<>', $this->object->getString());
168 * testing add param method
170 public function testAddParam()
172 $this->object->addParam(PMA_Message
::notice('test'));
173 $this->assertEquals(array(PMA_Message
::notice('test')), $this->object->getParams());
174 $this->object->addParam('test', true);
175 $this->assertEquals(array(PMA_Message
::notice('test'), 'test'), $this->object->getParams());
176 $this->object->addParam('test', false);
177 $this->assertEquals(array(PMA_Message
::notice('test'), 'test', PMA_Message
::notice('test')), $this->object->getParams());
181 * testing add string method
183 public function testAddString()
185 $this->object->addString('test', '*');
186 $this->assertEquals(array('*', PMA_Message
::notice('test')), $this->object->getAddedMessages());
187 $this->object->addString('test', '');
188 $this->assertEquals(array('*', PMA_Message
::notice('test'), '', PMA_Message
::notice('test')), $this->object->getAddedMessages());
192 * testing add message method
194 public function testAddMessage()
196 $this->object->addMessage('test', '');
197 $this->assertEquals(array(PMA_Message
::rawNotice('test')), $this->object->getAddedMessages());
198 $this->object->addMessage('test');
199 $this->assertEquals(array(PMA_Message
::rawNotice('test'), ' ', PMA_Message
::rawNotice('test')), $this->object->getAddedMessages());
203 * testing add messages method
205 public function testAddMessages()
208 $messages[] = "Test1";
209 $messages[] = new PMA_Message("PMA_Test2", PMA_Message
::ERROR
);
210 $messages[] = "Test3";
211 $this->object->addMessages($messages, '');
213 $this->assertEquals(array(PMA_Message
::rawNotice('Test1'), PMA_Message
::error("PMA_Test2"), PMA_Message
::rawNotice('Test3')), $this->object->getAddedMessages());
217 * testing setter of params
219 public function testSetParams()
221 $this->object->setParams('test&<>');
222 $this->assertEquals('test&<>', $this->object->getParams());
223 $this->object->setParams('test&<>', true);
224 $this->assertEquals('test&<>', $this->object->getParams());
228 * testing sanitize method
230 public function testSanitize()
232 $this->object->setString('test&string<>', false);
233 $this->assertEquals('test&string<>', PMA_Message
::sanitize($this->object));
234 $this->assertEquals(array('test&string<>', 'test&string<>'), PMA_Message
::sanitize(array($this->object, $this->object)));
237 public function decodeBBDataProvider()
240 array('[i]test[/i][i]aa[i/][em]test[/em]', '<em>test</em><em>aa[i/]<em>test</em>'),
241 array('[b]test[/b][strong]test[/strong]', '<strong>test</strong><strong>test</strong>'),
242 array('[tt]test[/tt][code]test[/code]', '<code>test</code><code>test</code>'),
243 array('[kbd]test[/kbd][br][sup]test[/sup]', '<kbd>test</kbd><br /><sup>test</sup>')
248 * testing decodeBB method
249 * @dataProvider decodeBBDataProvider
252 public function testDecodeBB($actual, $expected)
254 $this->assertEquals($expected, PMA_Message
::decodeBB($actual));
258 * testing format method
260 public function testFormat()
262 $this->assertEquals('test string', PMA_Message
::format('test string'));
263 $this->assertEquals('test string', PMA_Message
::format('test string', 'a'));
264 $this->assertEquals('test string', PMA_Message
::format('test string', array()));
265 $this->assertEquals('test string', PMA_Message
::format('%s string', array('test')));
270 * testing getHash method
272 public function testGetHash()
274 $this->object->setString('<&>test', false);
275 $this->object->setMessage('<&>test', false);
276 $this->assertEquals(md5(PMA_Message
::NOTICE
. '<&>test<&>test'), $this->object->getHash());
280 * getMessage test - with empty message and with non-empty string - not key in globals
281 * additional params are defined
283 public function testGetMessageWithoutMessageWithStringWithParams()
285 $this->object->setMessage('');
286 $this->object->setString('test string %s %s');
287 $this->object->addParam('test param 1');
288 $this->object->addParam('test param 2');
289 $this->assertEquals('test string test param 1 test param 2', $this->object->getMessage());
293 * getMessage test - with empty message and with empty string
295 public function testGetMessageWithoutMessageWithEmptyString()
297 $this->object->setMessage('');
298 $this->object->setString('');
299 $this->assertEquals('', $this->object->getMessage());
303 * getMessage test - with empty message and with string, which is key to GLOBALS
304 * additional messages are defined
306 public function testGetMessageWithoutMessageWithGlobalStringWithAddMessages()
308 $GLOBALS['key'] = 'test message';
309 $this->object->setMessage('');
310 $this->object->setString('key');
311 $this->object->addMessage('test message 2', ' - ');
312 $this->object->addMessage('test message 3', '&');
313 $this->assertEquals('test message - test message 2&test message 3', $this->object->getMessage());
314 unset($GLOBALS['key']);
318 * getMessage test - message is defined
319 * message with BBCode defined
321 public function testGetMessageWithMessageWithBBCode()
323 $this->object->setMessage('[kbd]test[/kbd] [a@./Documentation.html#cfg_Example@_blank]test[/a]');
324 $this->assertEquals('<kbd>test</kbd> <a href="./Documentation.html#cfg_Example" target="_blank">test</a>', $this->object->getMessage());
330 public function testGetLevel()
332 $this->assertEquals('notice', $this->object->getLevel());
333 $this->object->setNumber(PMA_Message
::SUCCESS
);
334 $this->assertEquals('success', $this->object->getLevel());
335 $this->object->setNumber(PMA_Message
::ERROR
);
336 $this->assertEquals('error', $this->object->getLevel());
340 * testing display method (output string and _is_displayed varible)
342 public function testDisplay()
344 $this->assertFalse($this->object->isDisplayed());
345 $this->object->setMessage('Test Message');
347 $this->expectOutputString('<div class="notice">Test Message</div>');
348 $this->object->display();
350 $this->assertTrue($this->object->isDisplayed());
356 public function testGetDisplay()
358 $this->object->setMessage('Test Message');
359 $this->assertEquals('<div class="notice">Test Message</div>', $this->object->getDisplay());
365 public function testIsDisplayed()
367 $this->assertFalse($this->object->isDisplayed(false));
368 $this->assertTrue($this->object->isDisplayed(true));
369 $this->assertTrue($this->object->isDisplayed(false));
372 public function providerAffectedRows(){
373 return array(array(1, '<div class="notice"> 1 row affected.</div>'));
374 return array(array(2, '<div class="notice"> 2 rows affected.</div>'));
375 return array(array(50000000000000, '<div class="notice"> 50000000000000 rows affected.</div>'));
381 * @dataProvider providerAffectedRows
383 public function testAffectedRows($rows, $output)
385 $this->object = new PMA_Message();
386 $msg = $this->object->affected_rows($rows);
387 echo $this->object->addMessage($msg);
388 $this->expectOutputString($output);
389 $this->object->display();
392 public function providerInsertedRows(){
393 return array(array(1, '<div class="notice"> 1 row inserted.</div>'));
394 return array(array(2, '<div class="notice"> 2 rows inserted.</div>'));
395 return array(array(50000000000000, '<div class="notice"> 50000000000000 rows inserted.</div>'));
401 * @dataProvider providerInsertedRows
403 public function testInsertedRows($rows, $output)
405 $this->object = new PMA_Message();
406 $msg = $this->object->inserted_rows($rows);
407 echo $this->object->addMessage($msg);
408 $this->expectOutputString($output);
409 $this->object->display();
412 public function providerDeletedRows(){
413 return array(array(1, '<div class="notice"> 1 row deleted.</div>'));
414 return array(array(2, '<div class="notice"> 2 rows deleted.</div>'));
415 return array(array(50000000000000, '<div class="notice"> 50000000000000 rows deleted.</div>'));
421 * @dataProvider providerDeletedRows
423 public function testDeletedRows($rows, $output)
425 $this->object = new PMA_Message();
426 $msg = $this->object->deleted_rows($rows);
427 echo $this->object->addMessage($msg);
428 $this->expectOutputString($output);
429 $this->object->display();