WebServiceInstitutionTest.php 6.6 KB
Newer Older
1 2 3 4
<?php
/**
 * Test the different web service protocols.
 *
5 6 7 8 9 10 11
 * @package    mahara
 * @subpackage auth-webservice
 * @author     Catalyst IT Ltd
 * @author     jerome@moodle.com
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
12 13
 */

Yuliya Bozhko's avatar
Yuliya Bozhko committed
14
require_once('WebServiceTestBase.class.php');
15 16 17 18

/**
 * functional tests for institutions
 */
Yuliya Bozhko's avatar
Yuliya Bozhko committed
19
class WebServiceInstitutionTest extends WebServiceTestBase {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    /**
    * local setup - outline the test functions in the framework
    *
    * @see TestBase::setUp()
    */
    public function setUp() {
        // default current user to admin
        parent::setUp();

        //protocols to test
        $this->testrest = true;
        $this->testxmlrpc = true;
        $this->testsoap = true;

        ////// READ-ONLY DB tests ////
        $this->readonlytests = array(
            'mahara_institution_get_members' => true,
            'mahara_institution_get_requests' => true,
        );

        ////// WRITE DB tests ////
        $this->writetests = array(
            'mahara_institution_add_members' => true,
            'mahara_institution_remove_members' => true,
            'mahara_institution_invite_members' => true,
            'mahara_institution_decline_members' => true,
        );

        ///// Authentication types ////
        $this->auths = array('token', 'user');

        //performance testing: number of time the web service are run
        $this->iteration = 1;

    }

    ///// WEB SERVICE TEST FUNCTIONS

    // simple get users by ID
    function mahara_institution_get_members($client) {
        error_log('getting members');
        require_once(get_config('docroot') . 'lib/searchlib.php');

        $institution = new Institution('mahara');
        $data = institutional_admin_user_search('', $institution, 0);

        $function = 'mahara_institution_get_members';
        $params = array('institution' => 'mahara');
        $users = $client->call($function, $params);

        $this->assertEquals(count($users), $data['count']);
    }

    // simple get users by ID
    function mahara_institution_get_requests($client) {
        error_log('getting requests');
        require_once(get_config('docroot') . 'lib/searchlib.php');

        //Set test data
        $dbuser1 = $this->create_user1_for_update();
        $dbuser2 = $this->create_user2_for_update();

        $institution = new Institution($this->testinstitution);
        $institution->invite_users(array($dbuser1->id, $dbuser2->id));
        $dbinvites = get_records_array('usr_institution_request', 'institution', $this->testinstitution);

        $function = 'mahara_institution_get_requests';
        $params = array('institution' => $this->testinstitution);
        $users = $client->call($function, $params);

        $this->assertEquals(count($users), count($dbinvites));
    }

    // update user test
    function mahara_institution_invite_members($client) {
        error_log('invite members');

        //Set test data
        $dbuser1 = $this->create_user1_for_update();
        $dbuser2 = $this->create_user2_for_update();

        $this->clean_institution();

        //update the users by web service
        $function = 'mahara_institution_invite_members';
        $params = array('institution' => $this->testinstitution, 'users' => array(array('id' => $dbuser1->id), array('id' => $dbuser2->id),));
        $client->call($function, $params);

        $dbinvites = get_records_array('usr_institution_request', 'institution', $this->testinstitution);

        //compare DB user with the test data
        $this->assertEquals(count($params['users']), count($dbinvites));
    }

    // update user test
    function mahara_institution_add_members($client) {
        error_log('add members');

        //Set test data
        $dbuser1 = $this->create_user1_for_update();
        $dbuser2 = $this->create_user2_for_update();

        $this->clean_institution();

        // members before
        $dbmembers_before = get_records_array('usr_institution', 'institution', $this->testinstitution);

        //update the users by web service
        $function = 'mahara_institution_add_members';
        $params = array('institution' => $this->testinstitution, 'users' => array(array('id' => $dbuser1->id), array('id' => $dbuser2->id),));
        $client->call($function, $params);

        $dbmembers = get_records_array('usr_institution', 'institution', $this->testinstitution);

        //compare DB user with the test data
        $this->assertEquals(count($params['users']), count($dbmembers));
    }


    // update user test
    function mahara_institution_remove_members($client) {
        error_log('remove members');

        //Set test data
        $dbuser1 = $this->create_user1_for_update();
        $dbuser2 = $this->create_user2_for_update();

        $this->clean_institution();

        $institution = new Institution($this->testinstitution);
        $institution->add_members(array($dbuser1->id, $dbuser2->id));
        $dbmembers = get_records_array('usr_institution', 'institution', $this->testinstitution);
        $this->assertEquals(2, count($dbmembers));

        //update the users by web service
        $function = 'mahara_institution_remove_members';
        $params = array('institution' => $this->testinstitution, 'users' => array(array('id' => $dbuser1->id), array('id' => $dbuser2->id),));
        $client->call($function, $params);

        $dbmembers = get_records_array('usr_institution', 'institution', $this->testinstitution);
        $this->assertTrue(empty($dbmembers));
    }

    // update user test
    function mahara_institution_decline_members($client) {
        error_log('decline members');

        //Set test data
        $dbuser1 = $this->create_user1_for_update();
        $dbuser2 = $this->create_user2_for_update();

        $this->clean_institution();

        $institution = new Institution($this->testinstitution);
        $institution->addRequestFromUser($dbuser1);
        $institution->addRequestFromUser($dbuser2);

        $dbinvites = get_records_array('usr_institution_request', 'institution', $this->testinstitution);
        $this->assertEquals(2, count($dbinvites));

        //update the users by web service
        $function = 'mahara_institution_decline_members';
        $params = array('institution' => $this->testinstitution, 'users' => array(array('id' => $dbuser1->id), array('id' => $dbuser2->id),));
        $client->call($function, $params);

        $dbinvites = get_records_array('usr_institution_request', 'institution', $this->testinstitution);
        $this->assertTrue(empty($dbinvites));
    }
}