Skip to content

Commit

Permalink
MDL-65033 mod_forum: Externallib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter authored and peterRd committed Apr 29, 2019
1 parent 24962ee commit 309e869
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 25 deletions.
9 changes: 8 additions & 1 deletion mod/forum/classes/local/entities/discussion.php
Expand Up @@ -300,6 +300,12 @@ public function has_group() : bool {
return $this->get_group_id() > 0;
}

/**
* Set the pinned value for this entity
*
* @param int $targetstate The state to change the pin to
* @return bool
*/
public function set_pinned(int $targetstate): bool {
if ($targetstate != $this->pinned) {
$this->pinned = $targetstate;
Expand All @@ -308,7 +314,6 @@ public function set_pinned(int $targetstate): bool {
return true;
}


/**
* Check if the discussion is timed.
*
Expand Down Expand Up @@ -336,6 +341,8 @@ public function is_timed_discussion_visible() : bool {
* @param discussion $discussion The discussion record
* @param context $forumcontext Forum context
* @param \stdClass $user The user to check the favourite against
*
* @return bool Whether or not the user has favourited the discussion
*/
public static function is_favourited(discussion $discussion, \context_module $forumcontext, \stdClass $user) {
$usercontext = \context_user::instance($user->id);
Expand Down
1 change: 1 addition & 0 deletions mod/forum/classes/local/factories/url.php
Expand Up @@ -455,6 +455,7 @@ public function get_discussion_subscribe_url(discussion_entity $discussion) : mo
'sesskey' => sesskey(),
'id' => $discussion->get_forum_id(),
'd' => $discussion->get_id()
]);
}

/**
Expand Down
8 changes: 0 additions & 8 deletions mod/forum/classes/local/renderers/discussion.php
Expand Up @@ -201,10 +201,6 @@ public function render(
$exporteddiscussion['html']['pindiscussion'] = $this->get_pin_discussion_html();
}

if ($capabilities['favourite']) {
$exporteddiscussion['html']['favouritediscussion'] = $this->get_favourite_discussion_html($exporteddiscussion);
}

return $this->renderer->render_from_template('mod_forum/forum_discussion', $exporteddiscussion);
}

Expand Down Expand Up @@ -355,10 +351,6 @@ private function get_pin_discussion_html() : string {
return $this->renderer->render($link);
}

private function get_favourite_discussion_html($exporteddiscussion) : string {
return $this->renderer->render_from_template('mod_forum/discussion_favourite_toggle', $exporteddiscussion);
}

/**
* Get the HTML to render the export discussion button.
*
Expand Down
1 change: 0 additions & 1 deletion mod/forum/classes/local/renderers/discussion_list.php
Expand Up @@ -171,7 +171,6 @@ public function render(stdClass $user, \cm_info $cm, ?int $groupid, ?int $sortor
),
'hasmore' => ($alldiscussionscount > $pagesize),
'notifications' => $this->get_notifications($user, $groupid),
'notifications' => $this->get_notifications($user, $groupid)
'settings' => [
'excludetext' => true,
'togglemoreicon' => true
Expand Down
29 changes: 21 additions & 8 deletions mod/forum/classes/local/vaults/discussion_list.php
Expand Up @@ -74,6 +74,11 @@ protected function get_table_alias() : string {
return 'd';
}

/**
* Get the favourite table alias
*
* @return string
*/
protected function get_favourite_alias() : string {
return 'favalias';
}
Expand All @@ -83,6 +88,8 @@ protected function get_favourite_alias() : string {
*
* @param string|null $wheresql Where conditions for the SQL
* @param string|null $sortsql Order by conditions for the SQL
* @param string|null $joinsql Additional join conditions for the sql
*
* @return string
*/
protected function generate_get_records_sql(string $wheresql = null, ?string $sortsql = null, ?string $joinsql = null) : string {
Expand Down Expand Up @@ -188,15 +195,20 @@ public function get_sort_order(?int $sortmethod) : string {

$alias = $this->get_table_alias();

$keyfield = "{$alias}.timemodified";
$direction = "DESC";
if ($sortmethod == self::SORTORDER_CREATED_DESC) {
$keyfield = "fp.created";
$direction = "DESC";
} else {
$keyfield = "{$alias}.timemodified";
$direction = "DESC";

if ($sortmethod == self::SORTORDER_OLDEST_FIRST) {
$direction = "ASC";
}
if ($sortmethod == self::SORTORDER_OLDEST_FIRST) {
$direction = "ASC";
}

if (!empty($CFG->forum_enabletimedposts)) {
$keyfield = "CASE WHEN {$keyfield} < {$alias}.timestart THEN {$alias}.timestart ELSE {$keyfield} END";
if (!empty($CFG->forum_enabletimedposts)) {
$keyfield = "CASE WHEN {$keyfield} < {$alias}.timestart THEN {$alias}.timestart ELSE {$keyfield} END";
}
}

return "{$alias}.pinned DESC, {$this->get_favourite_alias()}.id DESC, {$keyfield} {$direction}";
Expand Down Expand Up @@ -404,7 +416,8 @@ private function get_favourite_sql(): array {
$usercontext = \context_user::instance($USER->id);
$alias = $this->get_table_alias();
$ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
list($favsql, $favparams) = $ufservice->get_join_sql_by_type('mod_forum', 'discussions', $this->get_favourite_alias(), "$alias.id");
list($favsql, $favparams) = $ufservice->get_join_sql_by_type('mod_forum', 'discussions',
$this->get_favourite_alias(), "$alias.id");

return [$favsql, $favparams];
}
Expand Down
28 changes: 23 additions & 5 deletions mod/forum/externallib.php
Expand Up @@ -1097,6 +1097,14 @@ public static function add_discussion_post_returns() {
);
}

/**
* Toggle the favouriting value for the discussion provided
*
* @param int $forumid The forum id
* @param int $discussionid The discussion we need to favourite
* @param bool $targetstate The state of the favourite value
* @return array The exported discussion
*/
public static function toggle_favourite_state($forumid, $discussionid, $targetstate) {
global $DB, $PAGE, $USER;

Expand Down Expand Up @@ -1149,6 +1157,11 @@ public static function toggle_favourite_state_returns() {
return discussion_exporter::get_read_structure();
}

/**
* Defines the parameters for the toggle_favourite_state method
*
* @return external_function_parameters
*/
public static function toggle_favourite_state_parameters() {
return new external_function_parameters(
[
Expand Down Expand Up @@ -1638,17 +1651,19 @@ public static function set_pin_state($forumid, $discussionid, $targetstate) {
'targetstate' => $targetstate,
]);
$vaultfactory = mod_forum\local\container::get_vault_factory();
$managerfactory = mod_forum\local\container::get_manager_factory();
$forumvault = $vaultfactory->get_forum_vault();
$discussionvault = $vaultfactory->get_discussion_vault();
$forum = $forumvault->get_from_id($params['forumid']);
$capabilitymanager = $managerfactory->get_capability_manager($forum);

self::validate_context($forum->get_context());

$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
$forumrecord = $legacydatamapperfactory->get_forum_data_mapper()->to_legacy_object($forum);
if (!\mod_forum\subscriptions::is_subscribable($forumrecord)) {
if (!$capabilitymanager->can_pin_discussions($USER)) {
// Nothing to do. We won't actually output any content here though.
throw new \moodle_exception('cannotsubscribe', 'mod_forum');
throw new \moodle_exception('cannotpindiscussions', 'mod_forum');
}

$discussion = $discussionvault->get_from_id($params['discussionid']);
Expand Down Expand Up @@ -1684,9 +1699,12 @@ public static function set_lock_state_parameters() {
public static function set_pin_state_parameters() {
return new external_function_parameters(
[
'forumid' => new external_value(PARAM_INT, 'Forum that the discussion is in', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
'discussionid' => new external_value(PARAM_INT, 'The discussion to pin or unpin', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
'targetstate' => new external_value(PARAM_INT, 'The target state', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
'forumid' => new external_value(PARAM_INT, 'Forum that the discussion is in', VALUE_REQUIRED,
null, NULL_NOT_ALLOWED),
'discussionid' => new external_value(PARAM_INT, 'The discussion to pin or unpin', VALUE_REQUIRED,
null, NULL_NOT_ALLOWED),
'targetstate' => new external_value(PARAM_INT, 'The target state', VALUE_REQUIRED,
null, NULL_NOT_ALLOWED),
]
);
}
Expand Down
1 change: 1 addition & 0 deletions mod/forum/lang/en/forum.php
Expand Up @@ -75,6 +75,7 @@
$string['cannotmovetonotexist'] = 'You can\'t move to that forum - it doesn\'t exist!';
$string['cannotmovetonotfound'] = 'Target forum not found in this course.';
$string['cannotmovetosingleforum'] = 'Cannot move discussion to a simple single discussion forum';
$string['cannotpindiscussions'] = 'Sorry, you do not have the permission to pin discussions.';
$string['cannotpurgecachedrss'] = 'Could not purge the cached RSS feeds for the source and/or destination forum(s) - check your file permissionsforums';
$string['cannotremovesubscriber'] = 'Could not remove subscriber with id {$a} from this forum!';
$string['cannotreply'] = 'You cannot reply to this post';
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/tests/behat/behat_mod_forum.php
Expand Up @@ -136,7 +136,7 @@ public function i_navigate_to_post_in_forum($postsubject, $forumname) {
public function i_click_on_action_menu($discussion) {
$this->execute('behat_general::i_click_on_in_the', [
"[data-container='discussion-tools'] [data-toggle='dropdown']", "css_element",
"//tr[@class='discussion-row' and contains(.,'$discussion')]", "xpath_element"
"//tr[@class='discussion' and contains(.,'$discussion')]", "xpath_element"
]);
}

Expand Down
2 changes: 1 addition & 1 deletion mod/forum/tests/entities_discussion_test.php
Expand Up @@ -198,7 +198,7 @@ public function test_is_favourited() {

$this->assertFalse(\mod_forum\local\entities\discussion::is_favourited($discussion, $contextmodule, $user));

// Toggle the favourite for discussion
// Toggle the favourite for discussion.
$usercontext = \context_user::instance($user->id);
$ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
$ufservice->create_favourite('mod_forum', 'discussions', $discussion->get_id(), $contextmodule);
Expand Down
46 changes: 46 additions & 0 deletions mod/forum/tests/externallib_test.php
Expand Up @@ -221,6 +221,52 @@ public function test_mod_forum_toggle_favourite_state() {
$this->assertFalse($response['userstate']['favourited']);
}

/**
* Test the toggle pin state
*/
public function test_mod_forum_set_pin_state() {
$this->resetAfterTest(true);

// Create a user.
$user = self::getDataGenerator()->create_user(array('trackforums' => 1));

// Set to the user.
self::setUser($user);

// Create courses to add the modules.
$course1 = self::getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user->id, $course1->id);

$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course1->id;
$record->trackingtype = FORUM_TRACKING_OFF;
$forum1 = self::getDataGenerator()->create_module('forum', $record);
$forum1->introfiles = [];

// Add discussions to the forums.
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user->id;
$record->forum = $forum1->id;
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

try {
$response = mod_forum_external::set_pin_state($forum1->id, $discussion1->id, 1);
} catch (Exception $e) {
$this->assertEquals('cannotpindiscussions', $e->errorcode);
}

self::setAdminUser();
$response = mod_forum_external::set_pin_state($forum1->id, $discussion1->id, 1);
$response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response);
$this->assertTrue($response['pinned']);

$response = mod_forum_external::set_pin_state($forum1->id, $discussion1->id, 0);
$response = external_api::clean_returnvalue(mod_forum_external::set_pin_state_returns(), $response);
$this->assertFalse($response['pinned']);
}

/**
* Test get forum posts
*/
Expand Down

0 comments on commit 309e869

Please sign in to comment.