Skip to content

Commit

Permalink
MDL-74506 block: Fix block upgrade to use correct parentcontextid
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Apr 15, 2022
1 parent 4ca5665 commit ed56f9a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/db/upgradelib.php
Expand Up @@ -1338,7 +1338,7 @@ function upgrade_block_set_defaultregion(
timemodified
) SELECT
:selectblockname AS blockname,
:selectparentcontext AS parentcontextid,
c.id AS parentcontextid,
0 AS showinsubcontexts,
:selectpagetypepattern AS pagetypepattern,
mp.id AS subpagepattern,
Expand All @@ -1347,6 +1347,7 @@ function upgrade_block_set_defaultregion(
:selecttimecreated AS timecreated,
:selecttimemodified AS timemodified
FROM {my_pages} mp
JOIN {context} c ON c.instanceid = mp.userid AND c.contextlevel = :contextuser
WHERE mp.id NOT IN (
SELECT mpi.id FROM {my_pages} mpi
JOIN {block_instances} bi
Expand All @@ -1362,7 +1363,7 @@ function upgrade_block_set_defaultregion(
$context = context_system::instance();
$result = $DB->execute($sql, [
'selectblockname' => $blockname,
'selectparentcontext' => $context->id,
'contextuser' => CONTEXT_USER,
'selectpagetypepattern' => $pagetypepattern,
'selectdefaultregion' => $newdefaultregion,
'selecttimecreated' => time(),
Expand Down
8 changes: 7 additions & 1 deletion lib/tests/db/upgradelib_test.php
Expand Up @@ -180,7 +180,7 @@ public function test_upgrade_block_set_defaultregion_create_missing(): void {
// Any dashboards which are missing the block will have it created by the operation.
upgrade_block_set_defaultregion('calendar_month', '__default', 'my-index', 'content');

// Each of the dashboards should not have a block instance of the calendar_month block in the 'content' region
// Each of the dashboards should now have a block instance of the calendar_month block in the 'content' region
// on 'my-index' only.
foreach ($dashboards as $dashboardid) {
// Only one block should have been created.
Expand All @@ -193,6 +193,12 @@ public function test_upgrade_block_set_defaultregion_create_missing(): void {
$this->assertEquals('calendar_month', $theblock->blockname);
$this->assertEquals('content', $theblock->defaultregion);
$this->assertEquals('my-index', $theblock->pagetypepattern);

// Fetch the user details.
$dashboard = $DB->get_record('my_pages', ['id' => $dashboardid]);
$usercontext = \context_user::instance($dashboard->userid);

$this->assertEquals($usercontext->id, $theblock->parentcontextid);
}

// Enusre that there are no blocks on the mycourses page.
Expand Down

0 comments on commit ed56f9a

Please sign in to comment.