Commit b4b8463c authored by Son Nguyen's avatar Son Nguyen Committed by Aaron Wells
Browse files

Implement the behat fixture for creating pages (Bug 1400199)



Change-Id: Idd3a999dcc0321d1c74880cfcddb50bd094ecfb5
Signed-off-by: default avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent f7a8a7fd
......@@ -131,7 +131,7 @@ function group_user_can_edit_views($group, $userid=null) {
return true;
}
if (!is_logged_in()) {
if (empty($userid) && !is_logged_in()) {
return false;
}
......
......@@ -93,6 +93,101 @@ EOD;
}
/**
* Gets the user id from it's username.
* @param string $username
* @return int the user id
* = false if not exist
*/
protected function get_user_id($username) {
if (($res = get_records_sql_array('SELECT id FROM {usr} WHERE LOWER(TRIM(username)) = ?', array(strtolower(trim($username)))))
&& count($res) === 1) {
return $res[0]->id;
}
return false;
}
/**
* Gets the group id from it's name.
* @param string $groupname
* @return int the group id
* = false if not exist
*/
protected function get_group_id($groupname) {
if (($res = get_records_sql_array('SELECT id FROM {group} WHERE LOWER(TRIM(name)) = ?', array(strtolower(trim($groupname)))))
&& count($res) === 1) {
return $res[0]->id;
}
return false;
}
/**
* Gets the institution id from it's name.
* @param string $instname
* @return int the institution id
* = false if not exist
*/
protected function get_institution_id($instname) {
if (($res = get_records_sql_array('SELECT id FROM {institution} WHERE name = ?', array($instname)))
&& count($res) === 1) {
return $res[0]->id;
}
return false;
}
/**
* Gets the id of one site administrator.
* @return int the admin id
* = false if not exist
*/
protected function get_first_site_admin_id() {
if ($admins = get_records_sql_array('
SELECT u.id
FROM {usr} u
WHERE u.admin = 1 AND u.active = 1', array())) {
return $admins[0]->id;
}
return false;
}
/**
* Gets the id of one administrator of the institution given by name.
* @param string $instname
* @return int the admin id
* = false if not exist
*/
protected function get_first_institution_admin_id($instname) {
if ($admins = get_records_sql_array('
SELECT u.id
FROM {usr} u
INNER JOIN {usr_institution} ui ON ui.usr = u.id
WHERE ui.institution = ?
AND ui.admin = 1
AND u.active = 1', array($instname))) {
return $admins[0]->id;
}
return false;
}
/**
* Gets the id of one administrator of the group given by ID.
* @param int $groupid
* @return int the group admin id
* = false if not exist
*/
protected function get_first_group_admin_id($groupid) {
if ($admins = get_records_sql_array('
SELECT u.id
FROM {usr} u
INNER JOIN {group_member} gm ON gm.member = u.id
WHERE gm.group = ?
AND gm.role = ?
AND u.active = 1', array($groupid, 'admin'))) {
return $admins[0]->id;
}
return false;
}
/**
* Create a test user
* @param array $record
......@@ -306,6 +401,10 @@ EOD;
*/
public function create_institution($record) {
// Data validation
if (empty($record['name']) || !preg_match('/^[a-zA-Z]{1,255}$/', $record['name'])) {
throw new SystemException("Invalid institution name '" . $record['name'] .
"'. The institution name is entered for system database identification only and must be a single text word without numbers or symbols.");
}
if (!empty($record['name']) && record_exists('institution', 'name', $record['name'])) {
throw new SystemException("Invalid institution name '" . $record['name'] . "'. " . get_string('institutionnamealreadytaken', 'admin'));
}
......@@ -399,4 +498,60 @@ EOD;
db_commit();
}
/**
* Create an empty view
* @param array $record
* @throws SystemException if creating failed
* @return int new view id
*/
public function create_view($record) {
switch ($record['ownertype']) {
case 'institution':
if (empty($record['ownername'])) {
$record['institution'] = 'mahara';
break;
}
if ($institutionid = $this->get_institution_id($record['ownername'])) {
$record['institution'] = $record['ownername'];
// Find one of the institution admins
if (!$userid = $this->get_first_institution_admin_id($record['ownername'])) {
// Find one of site admins
$userid = $this->get_first_site_admin_id();
}
}
else {
throw new SystemException("The institution '" . $record['ownername'] . "' does not exist.");
}
break;
case 'group':
if ($groupid = $this->get_group_id($record['ownername'])) {
$record['group'] = $groupid;
// Find one of the group admins
if (!$userid = $this->get_first_group_admin_id($groupid)) {
throw new SystemException("The group '" . $record['ownername'] . "' must have at least one administrator.");
}
}
else {
throw new SystemException("The group '" . $record['ownername'] . "' does not exist.");
}
break;
case 'user':
default:
if ($ownerid = get_field('usr', 'id', 'username', $record['ownername'])) {
$record['owner'] = $ownerid;
// Find one of the site admins
$userid = $this->get_first_site_admin_id();
}
else {
throw new SystemException("The user '" . $record['ownername'] . "' does not exist.");
}
break;
}
if (empty($userid)) {
$userid = $this->get_first_site_admin_id();
}
$view = View::create($record, $userid);
}
}
......@@ -384,7 +384,6 @@ abstract class TestingUtil {
db_begin();
$prefix = get_config('dbprefix');
if (is_postgres()) {
$queries = array();
foreach ($data as $table => $records) {
if (isset($structure[$table]['ID'])
&& !empty($structure[$table]['ID']->auto_increment)
......@@ -396,12 +395,9 @@ abstract class TestingUtil {
$lastrecord = end($records);
$nextid = $lastrecord->id + 1;
}
$queries[] = "ALTER SEQUENCE {$prefix}{$table}_id_seq RESTART WITH $nextid";
execute_sql("ALTER SEQUENCE {$prefix}{$table}_id_seq RESTART WITH $nextid");
}
}
if ($queries) {
execute_sql_arr(implode(';', $queries));
}
}
else if (is_mysql()) {
......
......@@ -97,6 +97,17 @@ class BehatDataGenerators extends BehatBase {
),
'required' => array('name', 'displayname')
),
'pages' => array(
'datagenerator' => 'view',
'available' => array(
'title' => 'text',
'description' => 'text',
'ownertype' => 'text',
'ownername' => 'text',
'layout' => 'text',
),
'required' => array('title', 'ownertype', 'ownername'),
),
'group memberships' => array(
'datagenerator' => 'group_membership',
'required' => array('username', 'groupname', 'role')
......@@ -212,43 +223,4 @@ class BehatDataGenerators extends BehatBase {
}
/**
* Gets the user id from it's username.
* @throws MaharaBehatTestException
* @param string $username
* @return int
*/
protected function get_user_id($username) {
if (!$user = get_record('usr', 'username', $username)) {
throw new MaharaBehatTestException('The specified user with username "' . $username . '" does not exist');
}
return $user->id;
}
/**
* Gets the group id from it's name.
* @throws MaharaBehatTestException
* @param string $groupname
* @return int
*/
protected function get_group_id($groupname) {
if (!$group = get_record('group', 'name', $groupname)) {
throw new MaharaBehatTestException('The specified group with name "' . $groupname . '" does not exist');
}
return $group->id;
}
/**
* Gets the institution id from it's name.
* @throws MaharaBehatTestException
* @param string $instname
* @return int
*/
protected function get_institution_id($instname) {
if (!$institution = get_record('institution', 'name', $instname)) {
throw new MaharaBehatTestException('The specified institution with name "' . $instname . '" does not exist');
}
return $institution->id;
}
}
......@@ -21,6 +21,7 @@
define('INTERNAL', 1);
define('ADMIN', 1);
define('CLI', 1);
define('INSTALLER', 1);
define('BEHAT_UTIL', 1);
// No access from web!
......
@javascript @core @page
Feature: Mahara users can manage their pages
As a mahara user/admin
I can see/edit/delete one of my page
I can see/edit/delete a page in my groups
I can see/edit/delete a page in my institution or site
Background:
Given the following "institutions" exist:
| name | displayname | registerallowed | registerconfirm |
| instone | Institution One | ON | OFF |
| insttwo | Institution Two | ON | OFF |
And the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| userA | Password1 | test01@example.com | Pete | Mc | mahara | internal | admin |
| userB | Password1 | test02@example.com | Son | Nguyen | instone | internal | admin |
| userC | Password1 | test03@example.com | Jack | Smith | insttwo | internal | staff |
| userD | Password1 | test04@example.com | Eden | Wilson | mahara | internal | member |
And the following "groups" exist:
| name | owner | description | grouptype | open | invitefriends | editroles | submittableto | allowarchives | members | staff |
| group 01 | userA | This is group 01 | standard | ON | ON | all | ON | ON | userB, userC | userD |
And the following "pages" exist:
| title | description| ownertype | ownername |
| Page 01 | This is the page 01 | user | userD |
| Page 02 | This is the page 02 | user | userD |
| Site Page 01 | This is the page 01 of the site | institution | mahara |
| Institution Page 01 | This is the page 01 of the Institution One| institution | instone |
| Group Page 01 | This is the page 01 of the group 01 | group | group 01 |
Scenario: List my portfolio pages
Given I log in as "userD" with password "Password1"
When I go to "view/index.php"
And I wait "1" seconds
Then I should see "Page 01"
And I should see "Page 02"
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment