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';
15 require_once 'libraries/php-gettext/gettext.inc';
17 class PMA_Message_test
extends PHPUnit_Extensions_OutputTestCase
26 * Sets up the fixture, for example, opens a network connection.
27 * This method is called before a test is executed.
31 protected function setUp()
33 $this->object = new PMA_Message
;
37 * Tears down the fixture, for example, closes a network connection.
38 * This method is called after a test is executed.
42 protected function tearDown()
47 * to String casting test
49 public function test__toString()
51 $this->object->setMessage('test<&>', true);
52 $this->assertEquals('test<&>', (string)$this->object);
58 public function testSuccess()
60 $this->object = new PMA_Message('test<&>', PMA_Message
::SUCCESS
);
61 $this->assertEquals($this->object, PMA_Message
::success('test<&>'));
62 $this->assertEquals('Your SQL query has been executed successfully', PMA_Message
::success()->getString());
68 public function testError()
70 $this->object = new PMA_Message('test<&>', PMA_Message
::ERROR
);
71 $this->assertEquals($this->object, PMA_Message
::error('test<&>'));
72 $this->assertEquals('Error', PMA_Message
::error()->getString());
78 public function testNotice()
80 $this->object = new PMA_Message('test<&>', PMA_Message
::NOTICE
);
81 $this->assertEquals($this->object, PMA_Message
::notice('test<&>'));
85 * test rawError method
87 public function testRawError()
89 $this->object = new PMA_Message('', PMA_Message
::ERROR
);
90 $this->object->setMessage('test<&>');
92 $this->assertEquals($this->object, PMA_Message
::rawError('test<&>'));
96 * test rawNotice method
98 public function testRawNotice()
100 $this->object = new PMA_Message('', PMA_Message
::NOTICE
);
101 $this->object->setMessage('test<&>');
103 $this->assertEquals($this->object, PMA_Message
::rawNotice('test<&>'));
107 * test rawSuccess method
109 public function testRawSuccess()
111 $this->object = new PMA_Message('', PMA_Message
::SUCCESS
);
112 $this->object->setMessage('test<&>');
114 $this->assertEquals($this->object, PMA_Message
::rawSuccess('test<&>'));
118 * testing isSuccess method
120 public function testIsSuccess()
122 $this->assertFalse($this->object->isSuccess());
123 $this->assertTrue($this->object->isSuccess(true));
127 * testing isNotice method
129 public function testIsNotice()
131 $this->assertTrue($this->object->isNotice());
132 $this->object->isError(true);
133 $this->assertFalse($this->object->isNotice());
134 $this->assertTrue($this->object->isNotice(true));
138 * testing isError method
140 public function testIsError()
142 $this->assertFalse($this->object->isError());
143 $this->assertTrue($this->object->isError(true));
147 * testign setter of message
149 public function testSetMessage()
151 $this->object->setMessage('test&<>', false);
152 $this->assertEquals('test&<>', $this->object->getMessage());
153 $this->object->setMessage('test&<>', true);
154 $this->assertEquals('test&<>', $this->object->getMessage());
158 * testing setter of string
160 public function testSetString()
162 $this->object->setString('test&<>', false);
163 $this->assertEquals('test&<>', $this->object->getString());
164 $this->object->setString('test&<>', true);
165 $this->assertEquals('test&<>', $this->object->getString());
169 * testing add param method
171 public function testAddParam()
173 $this->object->addParam(PMA_Message
::notice('test'));
174 $this->assertEquals(array(PMA_Message
::notice('test')), $this->object->getParams());
175 $this->object->addParam('test', true);
176 $this->assertEquals(array(PMA_Message
::notice('test'), 'test'), $this->object->getParams());
177 $this->object->addParam('test', false);
178 $this->assertEquals(array(PMA_Message
::notice('test'), 'test', PMA_Message
::notice('test')), $this->object->getParams());
182 * testing add string method
184 public function testAddString()
186 $this->object->addString('test', '*');
187 $this->assertEquals(array('*', PMA_Message
::notice('test')), $this->object->getAddedMessages());
188 $this->object->addString('test', '');
189 $this->assertEquals(array('*', PMA_Message
::notice('test'), '', PMA_Message
::notice('test')), $this->object->getAddedMessages());
193 * testing add message method
195 public function testAddMessage()
197 $this->object->addMessage('test', '');
198 $this->assertEquals(array(PMA_Message
::rawNotice('test')), $this->object->getAddedMessages());
199 $this->object->addMessage('test');
200 $this->assertEquals(array(PMA_Message
::rawNotice('test'), ' ', PMA_Message
::rawNotice('test')), $this->object->getAddedMessages());
204 * testing add messages method
206 public function testAddMessages()
209 $messages[] = "Test1";
210 $messages[] = new PMA_Message("PMA_Test2", PMA_Message
::ERROR
);
211 $messages[] = "Test3";
212 $this->object->addMessages($messages, '');
214 $this->assertEquals(array(PMA_Message
::rawNotice('Test1'), PMA_Message
::error("PMA_Test2"), PMA_Message
::rawNotice('Test3')), $this->object->getAddedMessages());
218 * testing setter of params
220 public function testSetParams()
222 $this->object->setParams('test&<>');
223 $this->assertEquals('test&<>', $this->object->getParams());
224 $this->object->setParams('test&<>', true);
225 $this->assertEquals('test&<>', $this->object->getParams());
229 * testing sanitize method
231 public function testSanitize()
233 $this->object->setString('test&string<>', false);
234 $this->assertEquals('test&string<>', PMA_Message
::sanitize($this->object));
235 $this->assertEquals(array('test&string<>', 'test&string<>'), PMA_Message
::sanitize(array($this->object, $this->object)));
238 public function decodeBBDataProvider()
242 '[i]test[/i][i]aa[i/][em]test[/em]',
243 '<em>test</em><em>aa[i/]<em>test</em>'
246 '[b]test[/b][strong]test[/strong]',
247 '<strong>test</strong><strong>test</strong>'
250 '[tt]test[/tt][code]test[/code]',
251 '<code>test</code><code>test</code>'
254 '[kbd]test[/kbd][br][sup]test[/sup]',
255 '<kbd>test</kbd><br /><sup>test</sup>'
258 '[a@http://foo.bar/@Documentation]link[/a]',
259 '<a href="./url.php?url=http%3A%2F%2Ffoo.bar%2F&lang=en" target="Documentation">link</a>'
262 '[a@./non-existing@Documentation]link[/a]',
263 '[a@./non-existing@Documentation]link</a>'
266 '[a@./Documentation.html@Documentation]link[/a]',
267 '<a href="./Documentation.html" target="Documentation">link</a>'
273 * testing decodeBB method
274 * @dataProvider decodeBBDataProvider
277 public function testDecodeBB($actual, $expected)
279 $GLOBALS['lang'] = 'en';
280 $this->assertEquals($expected, PMA_Message
::decodeBB($actual));
284 * testing format method
286 public function testFormat()
288 $this->assertEquals('test string', PMA_Message
::format('test string'));
289 $this->assertEquals('test string', PMA_Message
::format('test string', 'a'));
290 $this->assertEquals('test string', PMA_Message
::format('test string', array()));
291 $this->assertEquals('test string', PMA_Message
::format('%s string', array('test')));
296 * testing getHash method
298 public function testGetHash()
300 $this->object->setString('<&>test', false);
301 $this->object->setMessage('<&>test', false);
302 $this->assertEquals(md5(PMA_Message
::NOTICE
. '<&>test<&>test'), $this->object->getHash());
306 * getMessage test - with empty message and with non-empty string - not key in globals
307 * additional params are defined
309 public function testGetMessageWithoutMessageWithStringWithParams()
311 $this->object->setMessage('');
312 $this->object->setString('test string %s %s');
313 $this->object->addParam('test param 1');
314 $this->object->addParam('test param 2');
315 $this->assertEquals('test string test param 1 test param 2', $this->object->getMessage());
319 * getMessage test - with empty message and with empty string
321 public function testGetMessageWithoutMessageWithEmptyString()
323 $this->object->setMessage('');
324 $this->object->setString('');
325 $this->assertEquals('', $this->object->getMessage());
329 * getMessage test - with empty message and with string, which is key to GLOBALS
330 * additional messages are defined
332 public function testGetMessageWithoutMessageWithGlobalStringWithAddMessages()
334 $GLOBALS['key'] = 'test message';
335 $this->object->setMessage('');
336 $this->object->setString('key');
337 $this->object->addMessage('test message 2', ' - ');
338 $this->object->addMessage('test message 3', '&');
339 $this->assertEquals('test message - test message 2&test message 3', $this->object->getMessage());
340 unset($GLOBALS['key']);
344 * getMessage test - message is defined
345 * message with BBCode defined
347 public function testGetMessageWithMessageWithBBCode()
349 $this->object->setMessage('[kbd]test[/kbd] [a@./Documentation.html#cfg_Example@_blank]test[/a]');
350 $this->assertEquals('<kbd>test</kbd> <a href="./Documentation.html#cfg_Example" target="_blank">test</a>', $this->object->getMessage());
356 public function testGetLevel()
358 $this->assertEquals('notice', $this->object->getLevel());
359 $this->object->setNumber(PMA_Message
::SUCCESS
);
360 $this->assertEquals('success', $this->object->getLevel());
361 $this->object->setNumber(PMA_Message
::ERROR
);
362 $this->assertEquals('error', $this->object->getLevel());
366 * testing display method (output string and _is_displayed varible)
368 public function testDisplay()
370 $this->assertFalse($this->object->isDisplayed());
371 $this->object->setMessage('Test Message');
373 $this->expectOutputString('<div class="notice">Test Message</div>');
374 $this->object->display();
376 $this->assertTrue($this->object->isDisplayed());
382 public function testGetDisplay()
384 $this->object->setMessage('Test Message');
385 $this->assertEquals('<div class="notice">Test Message</div>', $this->object->getDisplay());
391 public function testIsDisplayed()
393 $this->assertFalse($this->object->isDisplayed(false));
394 $this->assertTrue($this->object->isDisplayed(true));
395 $this->assertTrue($this->object->isDisplayed(false));
398 public function providerAffectedRows(){
399 return array(array(1, '<div class="notice"> 1 row affected.</div>'));
400 return array(array(2, '<div class="notice"> 2 rows affected.</div>'));
401 return array(array(50000000000000, '<div class="notice"> 50000000000000 rows affected.</div>'));
407 * @dataProvider providerAffectedRows
409 public function testAffectedRows($rows, $output)
411 $this->object = new PMA_Message();
412 $msg = $this->object->affected_rows($rows);
413 echo $this->object->addMessage($msg);
414 $this->expectOutputString($output);
415 $this->object->display();
418 public function providerInsertedRows(){
419 return array(array(1, '<div class="notice"> 1 row inserted.</div>'));
420 return array(array(2, '<div class="notice"> 2 rows inserted.</div>'));
421 return array(array(50000000000000, '<div class="notice"> 50000000000000 rows inserted.</div>'));
427 * @dataProvider providerInsertedRows
429 public function testInsertedRows($rows, $output)
431 $this->object = new PMA_Message();
432 $msg = $this->object->inserted_rows($rows);
433 echo $this->object->addMessage($msg);
434 $this->expectOutputString($output);
435 $this->object->display();
438 public function providerDeletedRows(){
439 return array(array(1, '<div class="notice"> 1 row deleted.</div>'));
440 return array(array(2, '<div class="notice"> 2 rows deleted.</div>'));
441 return array(array(50000000000000, '<div class="notice"> 50000000000000 rows deleted.</div>'));
447 * @dataProvider providerDeletedRows
449 public function testDeletedRows($rows, $output)
451 $this->object = new PMA_Message();
452 $msg = $this->object->deleted_rows($rows);
453 echo $this->object->addMessage($msg);
454 $this->expectOutputString($output);
455 $this->object->display();