Commit 579629f6 authored by Doris Tam's avatar Doris Tam

Bug 1810990: blocktype data: recentforumposts - create_forumposts() for bulk creation of forumposts

    additional:
        - tidied up external feeds
        - normalise() and validate fields in BehatGenerators to accept yes no

Change-Id: Idc3fbd67599b39969792ccbb0a271ef18f575bbe
parent 5d76fb65
......@@ -1693,6 +1693,111 @@ EOD;
));
}
/**
* A fixture to set up forum posts in bulk
*
* - if the topic doesn't exist, create a new one
* (fyi, a topic is just the first post in a thread ;)
* - if the forum doesn't exist, post in General Discussion and ignore the title
* - if no subject, it is the same name as the forum
*
* @param unknown $record
* @throws SystemException
*/
public function create_forumpost($record) {
$record['forum'] = trim($record['forum']);
$record['group'] = trim($record['group']);
$record['message'] = trim($record['message']);
$record['topic'] = trim($record['topic']);
$record['user'] = trim($record['user']);
$groupid;
$forumid;
$topicid;
$postid;
$userid;
$parentpostid = null;
$newtopic = false;
$newsubject = false;
if (!isset($record['topic'])) {
throw new SystemException("Missing a topic");
}
// check that the group exists
if (!$groupid = get_field('group', 'id', 'name',$record['group'] )) {
throw new SystemException("Invalid group '" . $record['group'] . "'");
}
// check the user exists and is part of the group i.e. can make a post
if ($userid = get_field('usr','id', 'username', $record['user'])) {
if (!get_field('group_member', 'member', 'group', $groupid, 'member', $userid)) {
throw new SystemException("The " . $record['user'] . " is not a member in the group " . $record['group']);
}
}
else {
throw new SystemException("The user " . $record['user'] . " doesn't exist");
}
// check the given forum exists else set to default forum General Discussion
if (!$forumid = get_field('interaction_instance', 'id', 'group', $groupid, 'title', $record['forum'])) {
// if the forum name doesn't exist, set the forumid to the default General discussion forum
$forumid = get_field('interaction_instance', 'id', 'group', $groupid, 'title', get_string('defaultforumtitle', 'interaction.forum'));
}
// Heads up, it will begin to get confusing here... so here is a brief explanation of my understanding:
// - the name of a forum is the title it is given when created
// - the name of a topic is the first subject of a post and is the parent of posts in responses to that parent post.
// Only the parent post holds the subject in the interaction_forum_post
// - if there is no subject given or the subject given is the same as the topic,
// the post responses have no subject, but they hold the postid of the the original parent post,
// - the name of a subject is either the parent post in a thread or a subparent in a thread with it's own subject
// - a subthread post with a new subject holds the parent as well as a subject title in the db
// check the given topic exists as a topic subject in the forums
if ($topicid = get_field('interaction_forum_post', 'topic', 'subject', $record['topic'])) {
$parentpostid = get_field('interaction_forum_post', 'id', 'subject', $record['topic']);
if (!empty($record['subject'])) {
// check that the given subject exists
if (!$subjectpostid = get_field('interaction_forum_post', 'id', 'subject', $record['subject'])) {
//new subject
$newsubject = true;
}
else {
//subject exists
$parentpostid = $subjectpostid;
}
}
}
// thread with given topic doesn't exist, so create a new topic with given topic
else {
$parentpostid = null;
$newtopic = true;
$record['subject'] = $record['topic'];
//create a new topic
$topicid = insert_record(
'interaction_forum_topic',
(object)array(
'forum' => $forumid,
'sticky' => 0,
'closed' => 0,
'sent' => 1
), 'id', true
);
}
$post = (object)array(
'topic' => $topicid,
'poster' => $userid,
'body' => $record['message'],
'ctime' => db_format_timestamp(time()),
'parent' => $parentpostid,
'subject' => ($newtopic || $newsubject) ? $record['subject'] : null
);
$postid = insert_record('interaction_forum_post', $post, 'id', true);
}
/**
* A fixture to set up messages in bulk.
* Currently it only supports setting friend request / accept internal notifications
......
......@@ -237,6 +237,18 @@ class BehatDataGenerators extends BehatBase {
'creator' => 'text'
),
'required' => array('title','description','group','creator')
),
'forumposts' => array(
'datagenerator' => 'forumpost',
'available' => array(
'group' => 'text',
'forum' => 'text',
'subject' => 'text',
'message' => 'text',
'user' => 'text',
'topic' => 'text',
),
'required' => array('group', 'message', 'user')
)
);
......@@ -246,18 +258,18 @@ class BehatDataGenerators extends BehatBase {
* @param array ('field' => 'values', ...) $record
* @return $record
*/
public function normalise(&$record) {
foreach ($record as &$value) {
$value = trim($value);
// Normalise boolean values
if (strtolower($value) == 'on' || $value == '1' || $value == 'yes') {
$value = true;
}
else if (strtolower($value) == 'off' || $value == '0' || $value == 'no') {
$value = false;
}
}
}
public function normalise(&$record) {
foreach ($record as &$value) {
$value = trim($value);
// Normalise boolean values
if (strtolower($value) == 'on' || $value == '1' || $value == 'yes' || $value == 'true') {
$value = true;
}
else if (strtolower($value) == 'off' || $value == '0' || $value == 'no' || $value == 'false') {
$value = false;
}
}
}
/**
* Validate field values in a given record
......@@ -274,7 +286,7 @@ class BehatDataGenerators extends BehatBase {
"All available fields are " . implode(',', array_keys($availablefields)));
}
if ($availablefields[$fieldname] == 'bool' && !is_bool($fieldvalue)) {
throw new MaharaBehatTestException("The value '" . $fieldvalue . "' of the field '" . $fieldname . "' must be a boolean ('ON'|'OFF', '1'|'0' are accepted boolean values).");
throw new MaharaBehatTestException("The value '" . $fieldvalue . "' of the field '" . $fieldname . "' must be a boolean ('ON'|'OFF', '1'|'0', 'true'|'false', 'yes'|'no' are accepted boolean values).");
}
if ($availablefields[$fieldname] == 'number' && !is_numeric($fieldvalue)) {
throw new MaharaBehatTestException("The value '" . $fieldvalue . "' of the field '" . $fieldname . "' must be a number.");
......
......@@ -19,6 +19,15 @@ Background:
| group | title | description | creator |
| Group1 | unicorns! | magic mahara unicorns| UserB |
And the following "forumposts" exist:
| group | forum | topic | subject | message | user |
| Group1 | unicorns! | topic one | | mahara unicorns unite! | UserB |
| Group1 | unicorns! | topic one | | yay! mahara unicorns unite! | UserB |
| Group1 | unicorns! | topic one | cheer on | woo! mahara unicorns unite! | UserB |
| Group1 | | topic one | cheer on | 10 papercranes, let's go! | UserB |
| Group1 | unicorns! | topic one | extra subj | 100 papercranes, let's go! | UserB |
| Group1 | unicorns! | |origami | 1000 papercranes, let's go! | UserB |
And the following "pages" exist:
| title | description | ownertype | ownername |
| Page UserA_00 | Page 01 | user | UserA |
......@@ -63,39 +72,41 @@ Background:
And the following "blocks" exist:
| title | type | page |retractable | data |
| My text 1 | text | Page UserA_00 | yes | This is some text |
| image jpg | image | Page UserA_00 | no | attachment=Image1.jpg; width=100 |
| image png | image | Page UserA_00 | no | attachment=Image2.png |
| My files 1 | filedownload | Page UserA_00 | auto | attachments=mahara_about.pdf |
| My files 2 | filedownload | Page UserA_00 | no | attachments=mahara_about.pdf,Image2.png |
| Rss news | externalfeed | Page UserA_00 | No | source=http://rss.nzherald.co.nz/rss/xml/nzhtsrsscid_000000698.xml |
| Rss food | externalfeed | Page UserA_00 | no | source=http://www.thekitchenmaid.com/feed |
| G image 3 | image | Page Grp1 | no | attachment=Image3.png |
| G files 2 | filedownload | Page Grp1 | no | attachments=mahara_about.pdf,Image2.png,testvid3.mp4,mahara.mp3 |
| nzslang | externalvideo | Page Grp1 | no | source=https://youtu.be/yRxFm70nOrY |
| my social | socialprofile | Page UserB_00 | no | sns=instagram,twitter,facebook,tumblr,pinterest |
| gall style1 | gallery | Page UserB_00 | no | attachments=Image1.jpg,Image3.png,Image3.png,Image2.png;imagesel=2;showdesc=yes;width=75;imagestyle=1;photoframe=1 |
| gall style2 | gallery | Page UserB_00 | yes | attachments=Image3.png,Image2.png,Image1.jpg;imagesel=2;showdesc=yes;width=75;imagestyle=2 |
| gall style3 | gallery | Page UserB_00 | yes | attachments=Image3.png,Image2.png,Image1.jpg;imagesel=2;showdesc=no;imagestyle=3;photoframe=0|
| myfolder | folder | Page UserB_00 | no | dirname=myfolder;attachments=mahara_about.pdf,Image2.png,Image1.jpg,Image3.png,mahara.mp3 |
| my html | html | Page UserB_00 | yes | attachment=test_html.html |
| my blog | blog | Page One | no | copytype=nocopy;count=5;journaltitle=journal1 |
| my blogpost | blogpost | Page One | no | copytype=nocopy;journaltitle=journal1;entrytitle=Entry Two |
| Comments | comment | Page One | | no configdata |
| PeerAssessmt| peerassessment | Page One | auto | no configdata |
| creativecoms| creativecommons | Page One | no | commercialuse=yes;license=3.0;allowmods=no |
| my nav | navigation | Page Two | no | collection=collection one;copytoall=yes |
| my plan | plans | Page Two | no | plans=Plan One,Plan Two;tasksdisplaycount=10 |
| internalm v | internalmedia | Page Two | no | attachment=testvid3.mp4 |
| internalm a | internalmedia | Page Two | no | attachment=mahara.mp3 |
| my pdf | pdf | Page Three | no | attachment=mahara_about.pdf |
| g rcnt posts| recentforumposts| Page Three | no | groupname=Group1; maxposts=-1 |
| title | type | page |retractable | data |
| My text 1 | text | Page UserA_00 | yes | This is some text |
| image jpg | image | Page UserA_00 | no | attachment=Image1.jpg; width=100 |
| image png | image | Page UserA_00 | no | attachment=Image2.png |
| My files 1 | filedownload | Page UserA_00 | auto | attachments=mahara_about.pdf |
| My files 2 | filedownload | Page UserA_00 | no | attachments=mahara_about.pdf,Image2.png |
| Rss news | externalfeed | Page UserA_00 | No | source=http://rss.nzherald.co.nz/rss/xml/nzhtsrsscid_000000698.xml |
| Rss food | externalfeed | Page UserA_00 | no | source=http://www.thekitchenmaid.com/feed |
| G image 3 | image | Page Grp1 | no | attachment=Image3.png |
| G files 2 | filedownload | Page Grp1 | no | attachments=mahara_about.pdf,Image2.png,testvid3.mp4,mahara.mp3 |
| nzslang | externalvideo | Page Grp1 | no | source=https://youtu.be/yRxFm70nOrY |
| my social | socialprofile | Page UserB_00 | no | sns=instagram,twitter,facebook,tumblr,pinterest,mysocialmedia |
| gall style1 | gallery | Page UserB_00 | no | attachments=Image1.jpg,Image3.png,Image3.png,Image2.png;imagesel=2;showdesc=yes;width=75;imagestyle=1;photoframe=1 |
| gall style2 | gallery | Page UserB_00 | yes | attachments=Image3.png,Image2.png,Image1.jpg;imagesel=2;showdesc=yes;width=75;imagestyle=2 |
| gall style3 | gallery | Page UserB_00 | yes | attachments=Image3.png,Image2.png,Image1.jpg;imagesel=2;showdesc=no;imagestyle=3;photoframe=0|
| myfolder | folder | Page UserB_00 | no | dirname=myfolder;attachments=mahara_about.pdf,Image2.png,Image1.jpg,Image3.png,mahara.mp3 |
| my html | html | Page UserB_00 | yes | attachment=test_html.html |
| my blog | blog | Page One | no | copytype=nocopy;count=5;journaltitle=journal1 |
| my blogpost | blogpost | Page One | no | copytype=nocopy;journaltitle=journal1;entrytitle=Entry Two |
| Comments | comment | Page One | | no configdata |
| PeerAssessmt| peerassessment | Page One | auto | no configdata |
| creativecoms| creativecommons| Page One | no | commercialuse=yes;license=3.0;allowmods=no |
| my nav | navigation | Page Two | no | collection=collection one;copytoall=yes |
| my plan | plans | Page Two | no | plans=Plan One,Plan Two;tasksdisplaycount=10 |
| internalm v | internalmedia | Page Three | no | attachment=testvid3.mp4 |
| internalm a | internalmedia | Page Three | no | attachment=mahara.mp3 |
| my pdf | pdf | Page Three | no | attachment=mahara_about.pdf |
| recentposts |recentforumposts| Page Three | no | groupname=Group1;maxposts=3 |
| nzslang | externalvideo | Page Three | no | source=https://youtu.be/yRxFm70nOrY |
Scenario: Login as admin to change upload settings
# To allow users to upload specific internal media types
......@@ -115,10 +126,8 @@ Scenario: Login as admin to change upload settings
| WEBM video file | 1 |
| WMV video file | 1 |
And I press "Save"
Then I log out
Scenario: Create Page with Blocks
Given I log in as "UserA" with password "Kupuh1pa!"
And I log out
Then 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