Commit aff1d621 authored by Doris Tam's avatar Doris Tam
Browse files

Bug 1810990: blocktypes: externalfeed,externalvideo,socialprofile,gallery,image

      - reorganised switch into smaller functions
      - indentation of code

Change-Id: I5fb8334056a4115007c2b53f81204a5396e035f1
parent b2a3704d
......@@ -316,7 +316,6 @@ class PluginBlocktypeExternalvideo extends MaharaCoreBlocktype {
return $values;
}
// If it's an unrecognised url, do nothing.
if (!$urldata = self::process_url($values['videoid'], $values['width'], $values['height'])) {
return $values;
......@@ -326,7 +325,7 @@ class PluginBlocktypeExternalvideo extends MaharaCoreBlocktype {
return array_merge($values, $urldata);
}
private static function process_url($url, $width=0, $height=0) {
public static function process_url($url, $width=0, $height=0) {
$sources = self::load_media_sources();
foreach ($sources as $name => $source) {
......
......@@ -683,90 +683,310 @@ EOD;
$order = !empty($order) ? $order : 1;
// build configdata
$configdata = array('retractable' => 0,
'retractedonload' => 0);
$configdata = $this->setup_retractable($record['retractable']);
$data = trim($record['data']);
if ($data) {
$values = array();
$functionname = 'generate_configdata_'.$record['type'];
$classname = 'TestingDataGenerator';
if (is_callable($classname . '::'.$functionname)) {
$result = call_static_method($classname, $functionname, $data, $ownertype, $ownerid);
$configdata = array_merge($configdata, $result);
}
else {
throw new SystemException("The blocktype {$record['type']} is not supported yet.");
}
// 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();
}
/**
* generate config data for the blocktype: text
* @param string inside data column in behat test
* @return array configdata of key and values for db table
**/
public static function generate_configdata_text($data) {
if (!$data) return;
$fields = explode(';', $data);
$configdata = array();
foreach ($fields as $field) {
list($key, $value) = explode('=', $field);
switch ($key) {
case 'attachment':
case 'image':
if ($key == 'image') {
$oldkey = 'image';
$key = 'artefactid';
if ($key == 'text') {
$configdata[$key] = $value;
}
else {
$oldkey = 'attachment';
$key = 'artefactids';
}
return $configdata;
}
/**
* generate config data for the blocktype: image
* @param string $data inside data column in behat test
* @param string $ownertype of user
* @param string $ownerid of the user
* @return array configdata of key and values for db table
**/
public static function generate_configdata_image($data, $ownertype, $ownerid) {
if (!$data) return;
$fields = explode(';', $data);
$configdata = array();
foreach ($fields as $field) {
list($key, $value) = explode('=', $field);
if ($key == 'image') {
// 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);
if (!$artefactimageid = get_field('artefact', 'id', 'title', $value, $ownertype, $ownerid)) {
$artefactimageid = TestingDataGenerator::upload_file($file=$value, $ownertype, $ownerid, 'image');
// Create folder and file inside it. then write contents into it...
$imagedir = get_config('dataroot') . ArtefactTypeFile::get_file_directory($artefactimageid);
if (!check_dir_exists($imagedir, true, true)) {
throw new SystemException("Unable to create folder $imagedir");
}
else {
$artobj = new ArtefactTypeFile(0, $artefact);
// Write contents to a file...
$imagepath = $imagedir . '/' . $artefactimageid;
$path = get_mahararoot_dir() . '/test/behat/upload_files/' . $value;
copy($path, $imagepath);
chmod($imagepath, get_config('filepermissions'));
}
$artobj->commit();
$newvalue = $artobj->get('id');
if (!$artefactimageid) {
throw new SystemException("Invalid attachment '" . $value . "'. No attachment by that name owned by " . $ownertype . " with id " . $ownerid);
}
}
$configdata = array('artefactid' => $artefactimageid);
}
}
return $configdata;
}
/**
* generate config data for the blocktype: filedownload
* @param string $data inside data column in behat test
* @param string $ownertype of user
* @param string $ownerid of the user
* @return array configdata of key and values for db table
**/
public static function generate_configdata_filedownload($data, $ownertype, $ownerid) {
if (!$data) return;
$fields = explode(';', $data);
$configdata = array();
foreach ($fields as $field) {
list($key, $value) = explode('=', $field);
if ($key == 'attachment') {
$path = get_mahararoot_dir() . '/test/behat/upload_files/' . $value;
$mimetype = mime_content_type($path);
list($media, $ext) = explode('/', $mimetype);
$mediatype = $media == 'application' ? 'attachment' : $media;
// we need to find the id of the item we are trying to attach and save it as artefactid
if (!$artefactid = get_field('artefact', 'id', 'title', $value, $ownertype, $ownerid)) {
$artefactid = TestingDataGenerator::upload_file($file=$value, $ownertype, $ownerid, $mediatype);
// 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");
$filedir = get_config('dataroot') . ArtefactTypeFile::get_file_directory($artefactid);
if (!check_dir_exists($filedir, true, true)) {
throw new SystemException("Unable to create folder $filedir");
}
else {
// Write contents to a file...
$imagepath = $imagedir . '/' . $newvalue;
$imagepath = $filedir . '/' . $artefactid;
$path = get_mahararoot_dir() . '/test/behat/upload_files/' . $value;
copy($path, $imagepath);
chmod($imagepath, get_config('filepermissions'));
}
if (!$newvalue) {
if (!$artefactid) {
throw new SystemException("Invalid attachment '" . $value . "'. No attachment by that name owned by " . $ownertype . " with id " . $ownerid);
}
}
if ($key == 'artefactids') {
$values[] = $newvalue;
$newvalue = $values;
$configdata['artefactids'][] = $artefactid;
}
}
return $configdata;
}
$value = $newvalue;
break;
default:
/**
* generate config data for the blocktype: rss feeds/external feeds
* @param string $data inside data column in behat test
* @return array configdata of key and values for db table
**/
public static function generate_configdata_externalfeed($data) {
if (!$data) return;
list($key, $value) = explode('=', $data);
if ($key == 'feed_location') {
$wheredata = array('url' => $value);
$feeddata = PluginBlocktypeExternalfeed::parse_feed($value);
$feeddata->content = serialize($feeddata->content);
$feeddata->image = serialize($feeddata->image);
$id = ensure_record_exists('blocktype_externalfeed_data', $wheredata, $feeddata, 'id', true);
}
return $configdata = array('feedid' => $id);
}
/**
* generate config data for the blocktype: external video
* @param string $data inside data column in behat test
* @return array configdata of key and values for db table
**/
public static function generate_configdata_externalvideo($data) {
if (!$data) return;
list($key, $value) = explode('=', $data);
if ($key == 'video_location') {
$sourceinfo = PluginBlocktypeExternalvideo::process_url($value);
return $sourceinfo;
}
}
/**
* generate config data for the blocktype: social profile
* @param string $data inside data column in behat test
* @param string $ownertype of user
* @param string $ownerid of the user
* @return array configdata of key and values for db table
**/
public static function generate_configdata_socialprofile($data, $ownertype, $ownerid) {
if (!$data) return;
list($key, $value) = explode('=', $data);
if ($key == 'social_profile') {
//split the values for multiple social profile creation
$medialist = explode(',', $value);
$value = array();
foreach($medialist as $media) {
$newprofile = new ArtefactTypeSocialprofile();
$newprofile->set('owner', $ownerid);
$newprofile->set('author',$ownerid);
$newprofile->set('title', $media);
$newprofile->set('description', $media);
$newprofile->set('note', $media);
$id = $newprofile->commit(); //update the contents of the artefact table only
$artefactid[] = $newprofile->get('id');
}
return $configdata = array('artefactids' => $artefactid);
}
}
/**
* generate config data for the blocktype: gallery
* @param string $data inside data column in behat test
* @param string $ownertype of user
* @param string $ownerid of the user
* @return array configdata of key and values for db table
**/
public static function generate_configdata_gallery($data, $ownertype, $ownerid) {
if (!$data) return;
$configdata = array();
//separate gallery_images, select, width, style etc.
$fields = explode(';', $data);
foreach ($fields as $field) {
list($key, $value) = explode('=', $field);
if ($key == 'gallery_images') {
$images = explode(',', $value);
$value = array();
foreach ($images as $image) {
if (!$artefactid = get_field('artefact', 'id', 'title', $image, $ownertype, $ownerid)) {
$artefactid = TestingDataGenerator::upload_file($image, $ownertype, $ownerid, 'image');
// Create folder and file inside it. then write contents into it...
$imagedir = get_config('dataroot') . ArtefactTypeFile::get_file_directory($artefactid);
if (!check_dir_exists($imagedir, true, true)) {
throw new SystemException("Unable to create folder $imagedir");
}
else {
// Write contents to a file...
$imagepath = $imagedir . '/' . $artefactid;
$path = get_mahararoot_dir() . '/test/behat/upload_files/' . $image;
copy($path, $imagepath);
chmod($imagepath, get_config('filepermissions'));
}
if (!$artefactid) {
throw new SystemException("Invalid attachment '" . $image . "'. No attachment by that name owned by " . $ownertype . " with id " . $ownerid);
}
}
$value[] = $artefactid;
}
$configdata['artefactids'] = $value;
}
if ($key == 'select' || $key == 'width' || $key == 'showdescription' || $key == 'style' || $key == 'photoframe' ) {
$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();
return $configdata;
}
/**
* set up configdata for retractable and retractable on load
*/
public function setup_retractable($setting) {
$configdata = array();
$configdata['retractable'] = $setting=='no' ? 0:1;;
$configdata['retractedonload'] = $setting=='auto'? 1:0;
return $configdata;
}
/**
* Create an image/file artefact
* @param string $file name
* @param string $ownertype i.e. institution, group, onwer
* @param int $ownerid
* @param $filetype of the upload file
* @return int artefactid
**/
public static function upload_file($file, $ownertype, $ownerid, $filetype) {
$ext = explode('.', $file);
$now = date("Y-m-d H:i:s");
$artefact = new stdClass();
$artefact->title = $file;
$artefact->oldextension = end($ext);
$artefact->$ownertype = $ownerid;
$artefact->author = $ownerid;
$artefact->atime = $now;
$artefact->ctime = $now;
$artefact->mtime = $now;
$artefactimageid;
if ($filetype == 'image') {
$path = get_mahararoot_dir() . '/test/behat/upload_files/' . $file;
$imageinfo = getimagesize($path);
$artefact->width = $imageinfo[0];
$artefact->height = $imageinfo[1];
$artimg = new ArtefactTypeImage(0, $artefact);
$artimg->commit();
$artefactimageid = $artimg->get('id');
}
else if ( $filetype == 'attachment') {
$artobj = new ArtefactTypeFile(0, $artefact);
$artobj->commit();
$artefactimageid = $artobj->get('id');
}
return $artefactimageid;
}
/**
......
......@@ -129,6 +129,8 @@ class BehatDataGenerators extends BehatBase {
'row' => 'text',
'column' => 'text',
'order' => 'text',
'retractable' => 'text',
),
'required' => array('title', 'type', 'page'),
),
......
......@@ -18,18 +18,28 @@ Background:
And the following "pages" exist:
| title | description | ownertype | ownername |
| Page UserA_00 | Page 01 | user | UserA |
| Page UserB_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 |
| title | type | page | row | column | order |retractable | data |
| My text 1 | text | Page UserA_00 | 1 | 1 | 1 | yes | text=This is sometext one |
| image jpg | image | Page UserA_00 | 1 | 1 | 2 | no | image=Image1.jpg |
| image png | image | Page UserA_00 | 1 | 1 | 3 | no | image=Image2.png |
| My files 1 | filedownload | Page UserA_00 | 1 | 2 | 1 | auto | attachment=mahara_about.pdf |
| My files 2 | filedownload | Page UserA_00 | 1 | 2 | 2 | no | attachment=mahara_about.pdf;attachment=Image2.png |
| Rss news | externalfeed | Page UserA_00 | 1 | 3 | 1 | no | feed_location=http://rss.nzherald.co.nz/rss/xml/nzhtsrsscid_000000698.xml |
| Rss food | externalfeed | Page UserA_00 | 1 | 3 | 2 | no | feed_location=http://www.thekitchenmaid.com/feed |
| G image 3 | image | Page Grp1 | 1 | 1 | 1 | no | image=Image3.png |
| G files 2 | filedownload | Page Grp1 | 1 | 1 | 2 | no | attachment=mahara_about.pdf;attachment=Image2.png |
| nzslang | externalvideo| Page Grp1 | 1 | 1 | 3 | no | video_location=https://youtu.be/yRxFm70nOrY |
| my social | socialprofile| Page UserB_00 | 1 | 1 | 1 | no | social_profile=instagram,twitter,facebook,tumblr,pinterest |
| gall style1| gallery | Page UserB_00 | 1 | 2 | 1 | no | gallery_images=Image1.jpg,Image3.png,Image3.png,Image2.png;select=1;showdescription=1;width=75;style=0;photoframe=1 |
| gall style2| gallery | Page UserB_00 | 1 | 2 | 2 | no | gallery_images=Image3.png,Image2.png,Image1.jpg;select=1;showdescription=1;width=75;style=1 |
| gall style3| gallery | Page UserB_00 | 1 | 2 | 3 | no | gallery_images=Image3.png,Image2.png,Image1.jpg;select=1;showdescription=1;style=2;photoframe=0|
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"
And I go to portfolio page "Page UserB_00"
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