Commit b2a3704d authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1810990: Beginning to get a behat background step for blocks



Currently tested against image/text/filedownload blocks for user/group

Change-Id: Ic8ee2010a9448bf16c4a5dc95684a6ab839d0e88
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 210713db
......@@ -636,6 +636,139 @@ EOD;
$view = View::create($record, $userid);
}
/**
* Create block content for existing view
* @param array $record
* @throws SystemException if creating failed
* @return int new block id
*/
public function create_block($record) {
global $USER;
$sql = "SELECT id FROM {view} WHERE LOWER(TRIM(title)) = ?";
$page = strtolower(trim($record['page']));
$ids = get_records_sql_array($sql, array($page));
if (!$ids || count($ids) > 1) {
throw new SystemException("Invalid page name '" . $record['page'] . "'. The page title does not exist, or is duplicated.");
}
else {
require_once('view.php');
$view = new View($ids[0]->id);
if (!empty($view->get('institution'))) {
$ownertype = 'institution';
$ownerid = $view->get('institution');
}
else if (!empty($view->get('group'))) {
$ownertype = 'group';
$ownerid = $view->get('group');
}
else {
$ownertype = 'owner';
$ownerid = $view->get('owner');
}
}
// We have a valid page so lets see if we can add a block to it
$blocktype = strtolower(trim($record['type']));
// Check that the blocktype exists and is active
if (!get_record('blocktype_installed', 'active', 1, 'name', $blocktype)) {
throw new SystemException("Invalid block type '" . $record['type'] . "'. The block type is either not installed or not active.");
}
$title = strtolower(trim($record['title']));
// Make sure we have row/column/order set or use defaults
$row = (int)trim($record['row']);
$row = !empty($row) ? $row : 1;
$column = (int)trim($record['column']);
$column = !empty($column) ? $column : 1;
$order = (int)trim($record['order']);
$order = !empty($order) ? $order : 1;
// build configdata
$configdata = array('retractable' => 0,
'retractedonload' => 0);
$data = trim($record['data']);
if ($data) {
$values = array();
$fields = explode(';', $data);
foreach ($fields as $field) {
list($key, $value) = explode('=', $field);
switch ($key) {
case 'attachment':
case 'image':
if ($key == 'image') {
$oldkey = 'image';
$key = 'artefactid';
}
else {
$oldkey = 'attachment';
$key = 'artefactids';
}
// we need to find the id of the item we are trying to attach and save it as artefactid
if (!$newvalue = get_field('artefact', 'id', 'title', $value, $ownertype, $ownerid)) {
// we need to ty and upload the file
$ext = explode('.', $value);
$now = date("Y-m-d H:i:s");
$artefact = new stdClass();
$artefact->title = $value;
$artefact->oldextension = end($ext);
$artefact->$ownertype = $ownerid;
$artefact->author = $ownerid;
$artefact->atime = $now;
$artefact->ctime = $now;
$artefact->mtime = $now;
if ($oldkey == 'image') {
$artobj = new ArtefactTypeImage(0, $artefact);
}
else {
$artobj = new ArtefactTypeFile(0, $artefact);
}
$artobj->commit();
$newvalue = $artobj->get('id');
// Create folder and file inside it. then write contents into it...
$imagedir = get_config('dataroot') . ArtefactTypeFile::get_file_directory($newvalue);
if (!check_dir_exists($imagedir, true, true)) {
throw new SystemException("Unable to create folder $imagedir");
}
else {
// Write contents to a file...
$imagepath = $imagedir . '/' . $newvalue;
$path = get_mahararoot_dir() . '/test/behat/upload_files/' . $value;
copy($path, $imagepath);
chmod($imagepath, get_config('filepermissions'));
}
if (!$newvalue) {
throw new SystemException("Invalid attachment '" . $value . "'. No attachment by that name owned by " . $ownertype . " with id " . $ownerid);
}
}
if ($key == 'artefactids') {
$values[] = $newvalue;
$newvalue = $values;
}
$value = $newvalue;
break;
default:
}
$configdata[$key] = $value;
}
}
// make new block
safe_require('blocktype', $blocktype);
$bi = new BlockInstance(0,
array(
'blocktype' => $blocktype,
'title' => $title,
'view' => $view->get('id'),
'row' => $row,
'column' => $column,
'order' => $order,
)
);
$bi->set('configdata', $configdata);
$bi->commit();
}
/**
* Create a collection of pages
* @param array $record
......
......@@ -119,6 +119,19 @@ class BehatDataGenerators extends BehatBase {
),
'required' => array('title', 'ownertype', 'ownername'),
),
'blocks' => array(
'datagenerator' => 'block',
'available' => array(
'title' => 'text',
'type' => 'text',
'data' => 'text',
'page' => 'text',
'row' => 'text',
'column' => 'text',
'order' => 'text',
),
'required' => array('title', 'type', 'page'),
),
'collections' => array(
'datagenerator' => 'collection',
'available' => array(
......
@javascript @core @blocktype
Feature: Creating a page with blocks
As a user
I want to add a page with blocks as a background step
As a group admin
I want to add a page with blocks as a background step
Background:
Given the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| UserA | Kupuh1pa! | UserA@example.org | Angela | User | mahara | internal | member |
| UserB | Kupuh1pa! | UserB@example.org | Bob | Boi | mahara | internal | member |
And the following "groups" exist:
| name | owner | description | grouptype | open | invitefriends | editroles | submittableto | allowarchives | members | staff |
| Group1 | UserB | Group1 owned by UserB | standard | ON | OFF | all | ON | OFF | UserA | |
And the following "pages" exist:
| title | description | ownertype | ownername |
| Page UserA_00 | Page 01 | user | UserA |
| Page Grp1 | Page 01 | group | Group1 |
And the following "blocks" exist:
| title | type | page | row | column | order | data |
| My text 1 | text | Page UserA_00 | 1 | 1 | 1 | text=This is sometext one |
| My image 1 | image | Page UserA_00 | 1 | 1 | 2 | image=Image2.png |
| My files 1 | filedownload | Page UserA_00 | 1 | 2 | 1 | attachment=mahara_about.pdf |
| My files 2 | filedownload | Page UserA_00 | 1 | 1 | 3 | attachment=mahara_about.pdf;attachment=Image2.png |
| G image 1 | image | Page Grp1 | 1 | 1 | 2 | image=Image2.png |
| G files 2 | filedownload | Page Grp1 | 1 | 1 | 3 | attachment=mahara_about.pdf;attachment=Image2.png |
Scenario: Create Page UserA_00 with text blocks
Given I log in as "UserA" with password "Kupuh1pa!"
And I go to portfolio page "Page UserA_00"
And I go to portfolio page "Page Grp1"
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