[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/external/tests/ -> external_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  defined('MOODLE_INTERNAL') || die();
  18  
  19  global $CFG;
  20  require_once($CFG->dirroot . '/lib/external/externallib.php');
  21  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  22  
  23  /**
  24   * External library functions unit tests
  25   *
  26   * @package    core
  27   * @category   phpunit
  28   * @copyright  2012 Jerome Mouneyrac
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class core_external_testcase extends externallib_advanced_testcase {
  32  
  33      /**
  34       * Test get_string
  35       */
  36      public function test_get_string() {
  37          $this->resetAfterTest(true);
  38  
  39          $service = new stdClass();
  40          $service->name = 'Dummy Service';
  41          $service->id = 12;
  42  
  43          // String with two parameters.
  44          $returnedstring = core_external::get_string('addservice', 'webservice',
  45                  array(array('name' => 'name', 'value' => $service->name),
  46                        array('name' => 'id', 'value' => $service->id)));
  47  
  48          // We need to execute the return values cleaning process to simulate the web service server.
  49          $returnedstring = external_api::clean_returnvalue(core_external::get_string_returns(), $returnedstring);
  50  
  51          $corestring = get_string('addservice', 'webservice', $service);
  52          $this->assertSame($corestring, $returnedstring);
  53  
  54          // String with one parameter.
  55          $acapname = 'A capability name';
  56          $returnedstring = core_external::get_string('missingrequiredcapability', 'webservice',
  57                  array(array('value' => $acapname)));
  58  
  59          // We need to execute the return values cleaning process to simulate the web service server.
  60          $returnedstring = external_api::clean_returnvalue(core_external::get_string_returns(), $returnedstring);
  61  
  62          $corestring = get_string('missingrequiredcapability', 'webservice', $acapname);
  63          $this->assertSame($corestring, $returnedstring);
  64  
  65          // String without parameters.
  66          $returnedstring = core_external::get_string('missingpassword', 'webservice');
  67  
  68          // We need to execute the return values cleaning process to simulate the web service server.
  69          $returnedstring = external_api::clean_returnvalue(core_external::get_string_returns(), $returnedstring);
  70  
  71          $corestring = get_string('missingpassword', 'webservice');
  72          $this->assertSame($corestring, $returnedstring);
  73  
  74          // String with two parameter but one is invalid (not named).
  75          $this->setExpectedException('moodle_exception');
  76          $returnedstring = core_external::get_string('addservice', 'webservice',
  77                  array(array('value' => $service->name),
  78                        array('name' => 'id', 'value' => $service->id)));
  79      }
  80  
  81      /**
  82       * Test get_strings
  83       */
  84      public function test_get_strings() {
  85          $this->resetAfterTest(true);
  86  
  87          $service = new stdClass();
  88          $service->name = 'Dummy Service';
  89          $service->id = 12;
  90  
  91          $returnedstrings = core_external::get_strings(
  92                  array(
  93                      array(
  94                          'stringid' => 'addservice', 'component' => 'webservice',
  95                          'stringparams' => array(array('name' => 'name', 'value' => $service->name),
  96                                array('name' => 'id', 'value' => $service->id)
  97                          )
  98                      ),
  99                      array('stringid' =>  'addaservice', 'component' => 'webservice')
 100                  ));
 101  
 102          // We need to execute the return values cleaning process to simulate the web service server.
 103          $returnedstrings = external_api::clean_returnvalue(core_external::get_strings_returns(), $returnedstrings);
 104  
 105          foreach($returnedstrings as $returnedstring) {
 106              $corestring = get_string($returnedstring['stringid'], $returnedstring['component'], $service);
 107              $this->assertSame($corestring, $returnedstring['string']);
 108          }
 109      }
 110  
 111      /**
 112       * Test get_component_strings
 113       */
 114      public function test_get_component_strings() {
 115          global $USER;
 116          $this->resetAfterTest(true);
 117  
 118          $stringmanager = get_string_manager();
 119  
 120          $wsstrings = $stringmanager->load_component_strings('webservice', current_language());
 121  
 122          $componentstrings = core_external::get_component_strings('webservice');
 123  
 124          // We need to execute the return values cleaning process to simulate the web service server.
 125          $componentstrings = external_api::clean_returnvalue(core_external::get_component_strings_returns(), $componentstrings);
 126  
 127          $this->assertEquals(count($componentstrings), count($wsstrings));
 128          foreach($componentstrings as $string) {
 129              $this->assertSame($string['string'], $wsstrings[$string['stringid']]);
 130          }
 131      }
 132  }


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