[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/message/output/airnotifier/tests/ -> externallib_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   * External airnotifier functions unit tests
  19   *
  20   * @package    message_airnotifier
  21   * @category   external
  22   * @copyright  2012 Jerome Mouneyrac
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  31  
  32  /**
  33   * External airnotifier functions unit tests
  34   *
  35   * @package    message_airnotifier
  36   * @category   external
  37   * @copyright  2012 Jerome Mouneyrac
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class message_airnotifier_external_testcase extends externallib_advanced_testcase {
  41  
  42      /**
  43       * Tests set up
  44       */
  45      protected function setUp() {
  46          global $CFG;
  47          require_once($CFG->dirroot . '/message/output/airnotifier/externallib.php');
  48      }
  49  
  50      /**
  51       * Test is_system_configured
  52       */
  53      public function test_is_system_configured() {
  54          global $DB;
  55  
  56          $this->resetAfterTest(true);
  57  
  58          $user  = self::getDataGenerator()->create_user();
  59          self::setUser($user);
  60  
  61          // In a clean installation, it should be not configured.
  62          $configured = message_airnotifier_external::is_system_configured();
  63          $this->assertEquals(0, $configured);
  64  
  65          // Fake configuration.
  66          set_config('airnotifieraccesskey', random_string());
  67          // Enable the plugin.
  68          $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
  69  
  70          $configured = message_airnotifier_external::is_system_configured();
  71          $this->assertEquals(1, $configured);
  72      }
  73  
  74      /**
  75       * Test are_notification_preferences_configured
  76       */
  77      public function test_are_notification_preferences_configured() {
  78  
  79          $this->resetAfterTest(true);
  80  
  81          $user1  = self::getDataGenerator()->create_user();
  82          $user2  = self::getDataGenerator()->create_user();
  83          $user3  = self::getDataGenerator()->create_user();
  84  
  85          self::setUser($user1);
  86  
  87          set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user1);
  88          set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'airnotifier', $user1);
  89          set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user2);
  90          set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user3);
  91  
  92          $params = array($user1->id, $user2->id, $user3->id);
  93  
  94          $preferences = message_airnotifier_external::are_notification_preferences_configured($params);
  95  
  96          $expected = array(
  97              array(
  98                  'userid' => $user1->id,
  99                  'configured' => 1
 100              )
 101          );
 102  
 103          $this->assertEquals(1, count($preferences['users']));
 104          $this->assertEquals($expected, $preferences['users']);
 105          $this->assertEquals(2, count($preferences['warnings']));
 106  
 107          // Now, remove one user.
 108          delete_user($user2);
 109          $preferences = message_airnotifier_external::are_notification_preferences_configured($params);
 110          $this->assertEquals(1, count($preferences['users']));
 111          $this->assertEquals($expected, $preferences['users']);
 112          $this->assertEquals(2, count($preferences['warnings']));
 113  
 114          // Now, remove one user1 preference (the user still has one prefernce for airnotifier).
 115          unset_user_preference('message_provider_moodle_instantmessage_loggedin', $user1);
 116          $preferences = message_airnotifier_external::are_notification_preferences_configured($params);
 117          $this->assertEquals($expected, $preferences['users']);
 118  
 119          // Delete the last user1 preference.
 120          unset_user_preference('message_provider_moodle_instantmessage_loggedoff', $user1);
 121          $preferences = message_airnotifier_external::are_notification_preferences_configured($params);
 122          $expected = array(
 123              array(
 124                  'userid' => $user1->id,
 125                  'configured' => 0
 126              )
 127          );
 128          $this->assertEquals($expected, $preferences['users']);
 129      }
 130  
 131  }


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