[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/phpunit/tests/ -> advanced_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * PHPUnit integration tests
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Test advanced_testcase extra features.
  31   *
  32   * @package    core
  33   * @category   phpunit
  34   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_phpunit_advanced_testcase extends advanced_testcase {
  38  
  39      public function test_debugging() {
  40          global $CFG;
  41          $this->resetAfterTest();
  42  
  43          debugging('hokus');
  44          $this->assertDebuggingCalled();
  45          debugging('pokus');
  46          $this->assertDebuggingCalled('pokus');
  47          debugging('pokus', DEBUG_MINIMAL);
  48          $this->assertDebuggingCalled('pokus', DEBUG_MINIMAL);
  49          $this->assertDebuggingNotCalled();
  50  
  51          debugging('a');
  52          debugging('b', DEBUG_MINIMAL);
  53          debugging('c', DEBUG_DEVELOPER);
  54          $debuggings = $this->getDebuggingMessages();
  55          $this->assertCount(3, $debuggings);
  56          $this->assertSame('a', $debuggings[0]->message);
  57          $this->assertSame(DEBUG_NORMAL, $debuggings[0]->level);
  58          $this->assertSame('b', $debuggings[1]->message);
  59          $this->assertSame(DEBUG_MINIMAL, $debuggings[1]->level);
  60          $this->assertSame('c', $debuggings[2]->message);
  61          $this->assertSame(DEBUG_DEVELOPER, $debuggings[2]->level);
  62  
  63          $this->resetDebugging();
  64          $this->assertDebuggingNotCalled();
  65          $debuggings = $this->getDebuggingMessages();
  66          $this->assertCount(0, $debuggings);
  67  
  68          set_debugging(DEBUG_NONE);
  69          debugging('hokus');
  70          $this->assertDebuggingNotCalled();
  71          set_debugging(DEBUG_DEVELOPER);
  72      }
  73  
  74      public function test_set_user() {
  75          global $USER, $DB;
  76  
  77          $this->assertEquals(0, $USER->id);
  78          $this->assertSame($_SESSION['USER'], $USER);
  79          $this->assertSame($GLOBALS['USER'], $USER);
  80  
  81          $user = $DB->get_record('user', array('id'=>2));
  82          $this->assertNotEmpty($user);
  83          $this->setUser($user);
  84          $this->assertEquals(2, $USER->id);
  85          $this->assertEquals(2, $_SESSION['USER']->id);
  86          $this->assertSame($_SESSION['USER'], $USER);
  87          $this->assertSame($GLOBALS['USER'], $USER);
  88  
  89          $USER->id = 3;
  90          $this->assertEquals(3, $USER->id);
  91          $this->assertEquals(3, $_SESSION['USER']->id);
  92          $this->assertSame($_SESSION['USER'], $USER);
  93          $this->assertSame($GLOBALS['USER'], $USER);
  94  
  95          \core\session\manager::set_user($user);
  96          $this->assertEquals(2, $USER->id);
  97          $this->assertEquals(2, $_SESSION['USER']->id);
  98          $this->assertSame($_SESSION['USER'], $USER);
  99          $this->assertSame($GLOBALS['USER'], $USER);
 100  
 101          $USER = $DB->get_record('user', array('id'=>1));
 102          $this->assertNotEmpty($USER);
 103          $this->assertEquals(1, $USER->id);
 104          $this->assertEquals(1, $_SESSION['USER']->id);
 105          $this->assertSame($_SESSION['USER'], $USER);
 106          $this->assertSame($GLOBALS['USER'], $USER);
 107  
 108          $this->setUser(null);
 109          $this->assertEquals(0, $USER->id);
 110          $this->assertSame($_SESSION['USER'], $USER);
 111          $this->assertSame($GLOBALS['USER'], $USER);
 112      }
 113  
 114      public function test_set_admin_user() {
 115          global $USER;
 116  
 117          $this->resetAfterTest();
 118  
 119          $this->setAdminUser();
 120          $this->assertEquals($USER->id, 2);
 121          $this->assertTrue(is_siteadmin());
 122      }
 123  
 124      public function test_set_guest_user() {
 125          global $USER;
 126  
 127          $this->resetAfterTest();
 128  
 129          $this->setGuestUser();
 130          $this->assertEquals($USER->id, 1);
 131          $this->assertTrue(isguestuser());
 132      }
 133  
 134      public function test_database_reset() {
 135          global $DB;
 136  
 137          $this->resetAfterTest();
 138  
 139          $this->preventResetByRollback();
 140  
 141          $this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site.
 142  
 143          // This is weird table - id is NOT a sequence here.
 144          $this->assertEquals(0, $DB->count_records('context_temp'));
 145          $DB->import_record('context_temp', array('id'=>5, 'path'=>'/1/2', 'depth'=>2));
 146          $record = $DB->get_record('context_temp', array());
 147          $this->assertEquals(5, $record->id);
 148  
 149          $this->assertEquals(0, $DB->count_records('user_preferences'));
 150          $originaldisplayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
 151          $this->assertEquals(1, $DB->count_records('user_preferences'));
 152  
 153          $numcourses = $DB->count_records('course');
 154          $course = $this->getDataGenerator()->create_course();
 155          $this->assertEquals($numcourses + 1, $DB->count_records('course'));
 156  
 157          $this->assertEquals(2, $DB->count_records('user'));
 158          $DB->delete_records('user', array('id'=>1));
 159          $this->assertEquals(1, $DB->count_records('user'));
 160  
 161          $this->resetAllData();
 162  
 163          $this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site.
 164          $this->assertEquals(0, $DB->count_records('context_temp')); // Only frontpage in new site.
 165  
 166          $numcourses = $DB->count_records('course');
 167          $course = $this->getDataGenerator()->create_course();
 168          $this->assertEquals($numcourses + 1, $DB->count_records('course'));
 169  
 170          $displayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
 171          $this->assertEquals($originaldisplayid, $displayid);
 172  
 173          $this->assertEquals(2, $DB->count_records('user'));
 174          $DB->delete_records('user', array('id'=>2));
 175          $user = $this->getDataGenerator()->create_user();
 176          $this->assertEquals(2, $DB->count_records('user'));
 177          $this->assertGreaterThan(2, $user->id);
 178  
 179          $this->resetAllData();
 180  
 181          $numcourses = $DB->count_records('course');
 182          $course = $this->getDataGenerator()->create_course();
 183          $this->assertEquals($numcourses + 1, $DB->count_records('course'));
 184  
 185          $this->assertEquals(2, $DB->count_records('user'));
 186          $DB->delete_records('user', array('id'=>2));
 187  
 188          $this->resetAllData();
 189  
 190          $numcourses = $DB->count_records('course');
 191          $course = $this->getDataGenerator()->create_course();
 192          $this->assertEquals($numcourses + 1, $DB->count_records('course'));
 193  
 194          $this->assertEquals(2, $DB->count_records('user'));
 195      }
 196  
 197      public function test_change_detection() {
 198          global $DB, $CFG, $COURSE, $SITE, $USER;
 199  
 200          $this->preventResetByRollback();
 201          phpunit_util::reset_all_data(true);
 202  
 203          // Database change.
 204          $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
 205          $DB->set_field('user', 'confirmed', 0, array('id'=>2));
 206          try {
 207              phpunit_util::reset_all_data(true);
 208          } catch (Exception $e) {
 209              $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
 210          }
 211          $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
 212  
 213          // Config change.
 214          $CFG->xx = 'yy';
 215          unset($CFG->admin);
 216          $CFG->rolesactive = 0;
 217          try {
 218              phpunit_util::reset_all_data(true);
 219          } catch (Exception $e) {
 220              $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
 221              $this->assertContains('xx', $e->getMessage());
 222              $this->assertContains('admin', $e->getMessage());
 223              $this->assertContains('rolesactive', $e->getMessage());
 224          }
 225          $this->assertFalse(isset($CFG->xx));
 226          $this->assertTrue(isset($CFG->admin));
 227          $this->assertEquals(1, $CFG->rolesactive);
 228  
 229          // _GET change.
 230          $_GET['__somethingthatwillnotnormallybepresent__'] = 'yy';
 231          phpunit_util::reset_all_data(true);
 232  
 233          $this->assertEquals(array(), $_GET);
 234  
 235          // _POST change.
 236          $_POST['__somethingthatwillnotnormallybepresent2__'] = 'yy';
 237          phpunit_util::reset_all_data(true);
 238          $this->assertEquals(array(), $_POST);
 239  
 240          // _FILES change.
 241          $_FILES['__somethingthatwillnotnormallybepresent3__'] = 'yy';
 242          phpunit_util::reset_all_data(true);
 243          $this->assertEquals(array(), $_FILES);
 244  
 245          // _REQUEST change.
 246          $_REQUEST['__somethingthatwillnotnormallybepresent4__'] = 'yy';
 247          phpunit_util::reset_all_data(true);
 248          $this->assertEquals(array(), $_REQUEST);
 249  
 250          // Silent changes.
 251          $_SERVER['xx'] = 'yy';
 252          phpunit_util::reset_all_data(true);
 253          $this->assertFalse(isset($_SERVER['xx']));
 254  
 255          // COURSE change.
 256          $SITE->id = 10;
 257          $COURSE = new stdClass();
 258          $COURSE->id = 7;
 259          try {
 260              phpunit_util::reset_all_data(true);
 261          } catch (Exception $e) {
 262              $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
 263              $this->assertEquals(1, $SITE->id);
 264              $this->assertSame($SITE, $COURSE);
 265              $this->assertSame($SITE, $COURSE);
 266          }
 267  
 268          // USER change.
 269          $this->setUser(2);
 270          try {
 271              phpunit_util::reset_all_data(true);
 272          } catch (Exception $e) {
 273              $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
 274              $this->assertEquals(0, $USER->id);
 275          }
 276      }
 277  
 278      public function test_getDataGenerator() {
 279          $generator = $this->getDataGenerator();
 280          $this->assertInstanceOf('testing_data_generator', $generator);
 281      }
 282  
 283      public function test_database_mock1() {
 284          global $DB;
 285  
 286          try {
 287              $DB->get_record('pokus', array());
 288              $this->fail('Exception expected when accessing non existent table');
 289          } catch (moodle_exception $e) {
 290              $this->assertInstanceOf('dml_exception', $e);
 291          }
 292          $DB = $this->getMock(get_class($DB));
 293          $this->assertNull($DB->get_record('pokus', array()));
 294          // Rest continues after reset.
 295      }
 296  
 297      public function test_database_mock2() {
 298          global $DB;
 299  
 300          // Now the database should be back to normal.
 301          $this->assertFalse($DB->get_record('user', array('id'=>9999)));
 302      }
 303  
 304      public function test_load_dataset() {
 305          global $DB;
 306  
 307          $this->resetAfterTest();
 308  
 309          $this->assertFalse($DB->record_exists('user', array('id'=>5)));
 310          $this->assertFalse($DB->record_exists('user', array('id'=>7)));
 311          $dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
 312          $this->loadDataSet($dataset);
 313          $this->assertTrue($DB->record_exists('user', array('id'=>5)));
 314          $this->assertTrue($DB->record_exists('user', array('id'=>7)));
 315          $user5 = $DB->get_record('user', array('id'=>5));
 316          $user7 = $DB->get_record('user', array('id'=>7));
 317          $this->assertSame('john.doe', $user5->username);
 318          $this->assertSame('jane.doe', $user7->username);
 319  
 320          $dataset = $this->createCsvDataSet(array('user'=>__DIR__.'/fixtures/sample_dataset.csv'));
 321          $this->loadDataSet($dataset);
 322          $this->assertEquals(8, $DB->get_field('user', 'id', array('username'=>'pepa.novak')));
 323          $this->assertEquals(9, $DB->get_field('user', 'id', array('username'=>'bozka.novakova')));
 324  
 325          $data = array(
 326              'user' => array(
 327                  array('username', 'email'),
 328                  array('top.secret', '[email protected]'),
 329                  array('low.secret', '[email protected]'),
 330              ),
 331          );
 332          $dataset = $this->createArrayDataSet($data);
 333          $this->loadDataSet($dataset);
 334          $this->assertTrue($DB->record_exists('user', array('email'=>'[email protected]')));
 335          $this->assertTrue($DB->record_exists('user', array('email'=>'[email protected]')));
 336  
 337          $data = array(
 338              'user' => array(
 339                  array('username'=>'noidea', 'email'=>'[email protected]'),
 340                  array('username'=>'onemore', 'email'=>'[email protected]'),
 341              ),
 342          );
 343          $dataset = $this->createArrayDataSet($data);
 344          $this->loadDataSet($dataset);
 345          $this->assertTrue($DB->record_exists('user', array('username'=>'noidea')));
 346          $this->assertTrue($DB->record_exists('user', array('username'=>'onemore')));
 347      }
 348  
 349      public function test_assert_time_current() {
 350          $this->assertTimeCurrent(time());
 351  
 352          $this->setCurrentTimeStart();
 353          $this->assertTimeCurrent(time());
 354          sleep(2);
 355          $this->assertTimeCurrent(time());
 356          $this->assertTimeCurrent(time()-1);
 357  
 358          try {
 359              $this->setCurrentTimeStart();
 360              $this->assertTimeCurrent(time()+10);
 361              $this->fail('Failed assert expected');
 362          } catch (Exception $e) {
 363              $this->assertInstanceOf('PHPUnit_Framework_ExpectationFailedException', $e);
 364          }
 365  
 366          try {
 367              $this->setCurrentTimeStart();
 368              $this->assertTimeCurrent(time()-10);
 369              $this->fail('Failed assert expected');
 370          } catch (Exception $e) {
 371              $this->assertInstanceOf('PHPUnit_Framework_ExpectationFailedException', $e);
 372          }
 373      }
 374  
 375      public function test_message_processors_reset() {
 376          global $DB;
 377  
 378          $this->resetAfterTest(true);
 379  
 380          // Get all processors first.
 381          $processors1 = get_message_processors();
 382  
 383          // Add a new message processor and get all processors again.
 384          $processor = new stdClass();
 385          $processor->name = 'test_processor';
 386          $processor->enabled = 1;
 387          $DB->insert_record('message_processors', $processor);
 388  
 389          $processors2 = get_message_processors();
 390  
 391          // Assert that new processor still haven't been added to the list.
 392          $this->assertSame($processors1, $processors2);
 393  
 394          // Reset message processors data.
 395          $processors3 = get_message_processors(false, true);
 396          // Now, list of processors should not be the same any more,
 397          // And we should have one more message processor in the list.
 398          $this->assertNotSame($processors1, $processors3);
 399          $this->assertEquals(count($processors1) + 1, count($processors3));
 400      }
 401  
 402      public function test_message_redirection() {
 403          $this->preventResetByRollback(); // Messaging is not compatible with transactions...
 404          $this->resetAfterTest(false);
 405  
 406          $user1 = $this->getDataGenerator()->create_user();
 407          $user2 = $this->getDataGenerator()->create_user();
 408  
 409          // Any core message will do here.
 410          $message1 = new stdClass();
 411          $message1->component         = 'moodle';
 412          $message1->name              = 'instantmessage';
 413          $message1->userfrom          = $user1;
 414          $message1->userto            = $user2;
 415          $message1->subject           = 'message subject 1';
 416          $message1->fullmessage       = 'message body';
 417          $message1->fullmessageformat = FORMAT_MARKDOWN;
 418          $message1->fullmessagehtml   = '<p>message body</p>';
 419          $message1->smallmessage      = 'small message';
 420          $message1->notification      = 0;
 421  
 422          $message2 = new stdClass();
 423          $message2->component         = 'moodle';
 424          $message2->name              = 'instantmessage';
 425          $message2->userfrom          = $user2;
 426          $message2->userto            = $user1;
 427          $message2->subject           = 'message subject 2';
 428          $message2->fullmessage       = 'message body';
 429          $message2->fullmessageformat = FORMAT_MARKDOWN;
 430          $message2->fullmessagehtml   = '<p>message body</p>';
 431          $message2->smallmessage      = 'small message';
 432          $message2->notification      = 0;
 433  
 434          // There should be debugging message without redirection.
 435          message_send($message1);
 436          $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
 437  
 438          // Sink should catch messages.
 439          $sink = $this->redirectMessages();
 440          $mid1 = message_send($message1);
 441          $mid2 = message_send($message2);
 442  
 443          $this->assertDebuggingNotCalled('message redirection must prevent debug messages from the message_send()');
 444          $this->assertEquals(2, $sink->count());
 445          $this->assertGreaterThanOrEqual(1, $mid1);
 446          $this->assertGreaterThanOrEqual($mid1, $mid2);
 447  
 448          $messages = $sink->get_messages();
 449          $this->assertInternalType('array', $messages);
 450          $this->assertCount(2, $messages);
 451          $this->assertEquals($mid1, $messages[0]->id);
 452          $this->assertEquals($message1->userto->id, $messages[0]->useridto);
 453          $this->assertEquals($message1->userfrom->id, $messages[0]->useridfrom);
 454          $this->assertEquals($message1->smallmessage, $messages[0]->smallmessage);
 455          $this->assertEquals($mid2, $messages[1]->id);
 456          $this->assertEquals($message2->userto->id, $messages[1]->useridto);
 457          $this->assertEquals($message2->userfrom->id, $messages[1]->useridfrom);
 458          $this->assertEquals($message2->smallmessage, $messages[1]->smallmessage);
 459  
 460          // Test resetting.
 461          $sink->clear();
 462          $messages = $sink->get_messages();
 463          $this->assertInternalType('array', $messages);
 464          $this->assertCount(0, $messages);
 465  
 466          message_send($message1);
 467          $messages = $sink->get_messages();
 468          $this->assertInternalType('array', $messages);
 469          $this->assertCount(1, $messages);
 470  
 471          // Test closing.
 472          $sink->close();
 473          $messages = $sink->get_messages();
 474          $this->assertInternalType('array', $messages);
 475          $this->assertCount(1, $messages, 'Messages in sink are supposed to stay there after close');
 476  
 477          // Test debugging is enabled again.
 478          message_send($message1);
 479          $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
 480  
 481          // Test invalid names and components.
 482  
 483          $sink = $this->redirectMessages();
 484  
 485          $message3 = new stdClass();
 486          $message3->component         = 'xxxx_yyyyy';
 487          $message3->name              = 'instantmessage';
 488          $message3->userfrom          = $user2;
 489          $message3->userto            = $user1;
 490          $message3->subject           = 'message subject 2';
 491          $message3->fullmessage       = 'message body';
 492          $message3->fullmessageformat = FORMAT_MARKDOWN;
 493          $message3->fullmessagehtml   = '<p>message body</p>';
 494          $message3->smallmessage      = 'small message';
 495          $message3->notification      = 0;
 496  
 497          try {
 498              message_send($message3);
 499              $this->fail('coding expcetion expected if invalid component specified');
 500          } catch (moodle_exception $e) {
 501              $this->assertInstanceOf('coding_exception', $e);
 502          }
 503  
 504          $message3->component = 'moodle';
 505          $message3->name      = 'yyyyyy';
 506          try {
 507              message_send($message3);
 508              $this->fail('coding expcetion expected if invalid name specified');
 509          } catch (moodle_exception $e) {
 510              $this->assertInstanceOf('coding_exception', $e);
 511          }
 512  
 513          message_send($message1);
 514          $this->assertEquals(1, $sink->count());
 515  
 516          // Test if sink can be carried over to next test.
 517          $this->assertTrue(phpunit_util::is_redirecting_messages());
 518          return $sink;
 519      }
 520  
 521      /**
 522       * @depends test_message_redirection
 523       */
 524      public function test_message_redirection_noreset($sink) {
 525          $this->preventResetByRollback(); // Messaging is not compatible with transactions...
 526          $this->resetAfterTest();
 527  
 528          $this->assertTrue(phpunit_util::is_redirecting_messages());
 529          $this->assertEquals(1, $sink->count());
 530  
 531          $message = new stdClass();
 532          $message->component         = 'moodle';
 533          $message->name              = 'instantmessage';
 534          $message->userfrom          = get_admin();
 535          $message->userto            = get_admin();
 536          $message->subject           = 'message subject 1';
 537          $message->fullmessage       = 'message body';
 538          $message->fullmessageformat = FORMAT_MARKDOWN;
 539          $message->fullmessagehtml   = '<p>message body</p>';
 540          $message->smallmessage      = 'small message';
 541          $message->notification      = 0;
 542  
 543          message_send($message);
 544          $this->assertEquals(2, $sink->count());
 545      }
 546  
 547      /**
 548       * @depends test_message_redirection_noreset
 549       */
 550      public function test_message_redirection_reset() {
 551          $this->assertFalse(phpunit_util::is_redirecting_messages(), 'Test reset must stop message redirection.');
 552      }
 553  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1