/** * Test LRS */ public function testLRS() { $lrs = new Lrs(); // Test title required. $values = array('title' => '', 'description' => \app\locker\helpers\Helpers::getRandomValue(), 'api' => array('basic_key' => \app\locker\helpers\Helpers::getRandomValue(), 'basic_secret' => \app\locker\helpers\Helpers::getRandomValue())); $validator = $lrs->validate($values); $this->assertTrue($validator->fails()); $this->assertFalse($validator->passes()); $values['title'] = \app\locker\helpers\Helpers::getRandomValue(); $validator = $lrs->validate($values); $this->assertTrue($validator->passes()); // Validate auth_service $values['auth_service_url'] = 'http://' . \app\locker\helpers\Helpers::getRandomValue() . '.adurolms.com'; $validator = $lrs->validate($values); $this->assertTrue($validator->passes()); // Add new lrs $lrs->title = $values['title']; $lrs->description = $values['description']; $lrs->api = $values['api']; $result = $lrs->save(); $this->assertTrue($result); // Load lrs from db $lrs_id = $lrs->_id; $db_lrs = Lrs::find($lrs_id); $this->assertEquals($db_lrs->_id, $lrs->_id); // Edit lrs $title = \app\locker\helpers\Helpers::getRandomValue(); $db_lrs->title = $title; $db_lrs->save(); $this->assertEquals($db_lrs->title, $title); // Delete lrs $db_lrs->delete(); $this->assertEquals(Lrs::find($lrs_id), NULL, 'delete lrs'); }
/** * Is user the owner of LRS (or site super admin) * * @return boolean * **/ public static function lrsOwner($lrs_id) { $lrs = \Lrs::find($lrs_id); if ($lrs->owner['_id'] == \Auth::user()->_id || \Auth::user()->role == 'super') { return true; } else { return false; } }
/** * test query analytics api. * * @return void */ public function testQueryAnalytics() { //testing response format $response = $this->call('GET', '/api/v1/query/analytics', array(), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData(); $this->assertEquals($data->version, 'v1'); $this->assertEquals($data->route, 'api/v1/query/analytics'); //FAILURES //testing params: filter // $filter = array( // 'object.definition.type' => 'http://activitystrea.ms/schema/2.0/badge' // ); // $response = $this->call('GET', '/api/v1/query/analytics', // array('filters' => json_encode($filter)), // array(), // array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], // 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); // $data = $response->getData()->data; // var_dump($data); //testing params: type:time $response = $this->call('GET', '/api/v1/query/analytics', array('type' => 'time'), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->count, 2); //testing params: type:user $response = $this->call('GET', '/api/v1/query/analytics', array('type' => 'user'), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $checkTypeUser = TRUE; foreach ($data as $value) { if (!in_array($value->data->name, array('quanvm', 'quanvm2'))) { $checkTypeUser = FALSE; } } $this->assertTRUE($checkTypeUser); //testing params: type:verb $response = $this->call('GET', '/api/v1/query/analytics', array('type' => 'verb'), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->data->id, "http://adlnet.gov/expapi/verbs/experienced"); /** * testing params: type:interval required type = time * need an static LRS and updated statement for this test */ $intervalDayLrs = Lrs::find('536b02d4c01f1325618b4567'); if ($intervalDayLrs) { $response = $this->call('GET', '/api/v1/query/analytics', array('interval' => 'Day'), array(), array('PHP_AUTH_USER' => $intervalDayLrs->api['basic_key'], 'PHP_AUTH_PW' => $intervalDayLrs->api['basic_secret'])); $data = $response->getData()->data; //need 2 statements which different day $this->assertEquals(count($data), 2); } $intervalMonthLrs = Lrs::find('536b03bbc01f13a6618b4567'); if ($intervalMonthLrs) { $response = $this->call('GET', '/api/v1/query/analytics', array('interval' => 'Month'), array(), array('PHP_AUTH_USER' => $intervalMonthLrs->api['basic_key'], 'PHP_AUTH_PW' => $intervalMonthLrs->api['basic_secret'])); $data = $response->getData()->data; //need 2 statements which different Month $this->assertEquals(count($data), 2); } // FAILURES // $intervalYearLrs = Lrs::find('536b057dc01f1377638b4567'); // if ($intervalYearLrs) { // $response = $this->call('GET', '/api/v1/query/analytics', // array('interval' => 'Year'), // array(), // array('PHP_AUTH_USER' => $intervalYearLrs->api['basic_key'], // 'PHP_AUTH_PW' => $intervalYearLrs->api['basic_secret'])); // $data = $response->getData()->data; // var_dump($data); // //need 2 statement which different Year // $this->assertEquals(count($data), 2); // } $intervalWeekLrs = Lrs::find('536b05ccc01f1392638b4567'); if ($intervalWeekLrs) { $response = $this->call('GET', '/api/v1/query/analytics', array('interval' => 'Year'), array(), array('PHP_AUTH_USER' => $intervalWeekLrs->api['basic_key'], 'PHP_AUTH_PW' => $intervalWeekLrs->api['basic_secret'])); $data = $response->getData()->data; //need 2 statements which different Week $this->assertEquals(count($data), 2); } //testing params: since $response = $this->call('GET', '/api/v1/query/analytics', array('since' => date('Y-m-d')), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->count, 2); $response = $this->call('GET', '/api/v1/query/analytics', array('since' => date('Y-m-d', strtotime("+1 day"))), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertTrue(empty($data)); //testing params: until $response = $this->call('GET', '/api/v1/query/analytics', array('until' => date('Y-m-d', strtotime("+1 day"))), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->count, 2); $response = $this->call('GET', '/api/v1/query/analytics', array('until' => date('Y-m-d', strtotime("-1 day"))), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertTrue(empty($data)); }
/** * Invite in a user. **/ public static function inviteUser($data) { //explode email addresses $emails = explode("\r\n", $data['emails']); foreach ($emails as $e) { $isMember = false; //make sure lower case $e = strtolower($e); //check it is a valid email address if (filter_var($e, FILTER_VALIDATE_EMAIL)) { //does the user already exist? If so, skip next step $user = \User::where('email', $e)->first(); $user_exists = false; //boolean used to determine if add to lrs email sent if (!$user) { //create a user account $user = new \User(); $user->name = $e; $user->email = $e; $user->verified = 'no'; $user->role = $data['role'] ? $data['role'] : 'observer'; $user->password = \Hash::make(base_convert(uniqid('pass', true), 10, 36)); $user->save(); } else { $user_exists = true; } //was an LRS id passed? If so, add user to that LRS as an observer if (isset($data['lrs'])) { $lrs = \Lrs::find($data['lrs']); //is the user already a member of the LRS? $isMember = \Locker\Helpers\Lrs::isMember($lrs->_id, $user->_id); //if lrs exists and user is not a member, add them if ($lrs && !$isMember) { $existing = $lrs->users; array_push($existing, array('_id' => $user->_id, 'email' => $user->email, 'role' => 'observer')); $lrs->users = $existing; $lrs->save(); } } //if user is already a member, exit here if ($isMember) { continue; } //determine which message to send to the user if ($user_exists && isset($lrs)) { //set data to use in email $set_data = array('sender' => \Auth::user(), 'lrs' => $lrs); //send out message to user \Mail::send('emails.lrsInvite', $set_data, function ($message) use($user) { $message->to($user->email, $user->name)->subject('You have been added to an LRS.'); }); } elseif ($user_exists) { //do nothing as they are already in the system } else { //if adding to lrs, get lrs title, otherwise use the site name isset($lrs) ? $title = 'the ' . $lrs->title . ' LRS' : ($title = \Site::first()->name . '\'s Learning Locker'); //set data to use in email $set_data = array('token' => User::setEmailToken($user, $user->email), 'custom_message' => $data['message'], 'title' => $title, 'sender' => \Auth::user()); //send out message to user \Mail::send('emails.invite', $set_data, function ($message) use($user) { $message->to($user->email, $user->name)->subject('You have been invited to join our LRS.'); }); } } } }
} //check current user is in the list of allowed users, or is super admin if (!in_array($user->_id, $get_users) && $user->role != 'super') { return Redirect::to('/'); } } else { return Redirect::to('/'); } }); // Checks for LRS edit access. Route::filter('edit.lrs', function ($route, $request) { //check to see if lrs id exists? $lrs = Lrs::find($route->parameter('id')); //if not, let's try the lrs parameter if (!$lrs) { $lrs = Lrs::find($route->parameter('lrs')); } $user = \Auth::user(); if ($lrs) { //get all users with admin access to the lrs foreach ($lrs->users as $u) { if ($u['role'] == 'admin') { $get_users[] = $u['_id']; } } //check current user is in the list of allowed users or is super if (!in_array($user->_id, $get_users) && $user->role != 'super') { return Redirect::to('/'); } } else { return Redirect::to('/');