diff --git a/cohort/tests/cohortlib_test.php b/cohort/tests/cohortlib_test.php index 107f40fc546..e92c2649072 100644 --- a/cohort/tests/cohortlib_test.php +++ b/cohort/tests/cohortlib_test.php @@ -739,109 +739,4 @@ class core_cohort_cohortlib_testcase extends advanced_testcase { $this->assertNotEmpty($updatedcohort->theme); $this->assertSame($cohort1->theme, $updatedcohort->theme); } - - /** - * Validate the theme value depending on the user theme and cohorts. - */ - public function test_cohort_get_user_theme() { - global $DB, $PAGE, $USER; - - $this->resetAfterTest(); - - // Enable cohort themes. - set_config('allowuserthemes', 1); - set_config('allowcohortthemes', 1); - - $systemctx = context_system::instance(); - - $usercases = $this->get_user_theme_cases(); - foreach ($usercases as $casename => $casevalues) { - set_config('theme', $casevalues['sitetheme']); - // Create user. - $user = $this->getDataGenerator()->create_user(array('theme' => $casevalues['usertheme'])); - - // Create cohorts and add user as member. - $cohorts = array(); - foreach ($casevalues['cohorts'] as $cohorttheme) { - $cohort = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'Cohort', - 'idnumber' => '', 'description' => '', 'theme' => $cohorttheme)); - $cohorts[] = $cohort; - cohort_add_member($cohort->id, $user->id); - } - - // Get the theme and compare to the expected. - $this->setUser($user); - // Initialise user theme. - $USER = get_complete_user_data('id', $user->id); - // Initialise site theme. - $PAGE->reset_theme_and_output(); - $PAGE->initialise_theme_and_output(); - $result = $PAGE->__get('theme')->name; - $this->assertEquals($casevalues['expected'], $result, $casename); - } - } - - /** - * Some user cases for validating the expected theme depending on the cohorts, site and user values. - * - * The result is an array of: - * 'User case description' => [ - * 'usertheme' => '', // User theme. - * 'sitetheme' => '', // Site theme. - * 'cohorts' => [], // Cohort themes. - * 'expected' => '', // Expected value returned by cohort_get_user_cohort_theme. - * ] - * - * @return array - */ - private function get_user_theme_cases() { - return [ - 'User not a member of any cohort' => [ - 'usertheme' => '', - 'sitetheme' => 'boost', - 'cohorts' => [], - 'expected' => 'boost', - ], - 'User member of one cohort which has a theme set' => [ - 'usertheme' => '', - 'sitetheme' => 'boost', - 'cohorts' => [ - 'clean', - ], - 'expected' => 'clean', - ], - 'User member of one cohort which has a theme set, and one without a theme' => [ - 'usertheme' => '', - 'sitetheme' => 'boost', - 'cohorts' => [ - 'clean', - '', - ], - 'expected' => 'clean', - ], - 'User member of one cohort which has a theme set, and one with a different theme' => [ - 'usertheme' => '', - 'sitetheme' => 'boost', - 'cohorts' => [ - 'clean', - 'someother', - ], - 'expected' => 'boost', - ], - 'User with a theme but not a member of any cohort' => [ - 'usertheme' => 'more', - 'sitetheme' => 'boost', - 'cohorts' => [], - 'expected' => 'more', - ], - 'User with a theme and member of one cohort which has a theme set' => [ - 'usertheme' => 'more', - 'sitetheme' => 'boost', - 'cohorts' => [ - 'clean', - ], - 'expected' => 'more', - ], - ]; - } } diff --git a/lib/tests/moodle_page_test.php b/lib/tests/moodle_page_test.php index 1f4091e1aef..514b3b13007 100644 --- a/lib/tests/moodle_page_test.php +++ b/lib/tests/moodle_page_test.php @@ -665,6 +665,112 @@ class core_moodle_page_testcase extends advanced_testcase { $footer = $OUTPUT->footer(); $this->assertEmpty($footer, 'cli output does not have a footer.'); } + + /** + * Validate the theme value depending on the user theme and cohorts. + * + * @dataProvider get_user_theme_provider + */ + public function test_cohort_get_user_theme($usertheme, $sitetheme, $cohortthemes, $expected) { + global $DB, $PAGE, $USER; + + $this->resetAfterTest(); + + // Enable cohort themes. + set_config('allowuserthemes', 1); + set_config('allowcohortthemes', 1); + + $systemctx = context_system::instance(); + + set_config('theme', $sitetheme); + // Create user. + $user = $this->getDataGenerator()->create_user(array('theme' => $usertheme)); + + // Create cohorts and add user as member. + $cohorts = array(); + foreach ($cohortthemes as $cohorttheme) { + $cohort = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'Cohort', + 'idnumber' => '', 'description' => '', 'theme' => $cohorttheme)); + $cohorts[] = $cohort; + cohort_add_member($cohort->id, $user->id); + } + + // Get the theme and compare to the expected. + $this->setUser($user); + + // Initialise user theme. + $USER = get_complete_user_data('id', $user->id); + + // Initialise site theme. + $PAGE->reset_theme_and_output(); + $PAGE->initialise_theme_and_output(); + $result = $PAGE->theme->name; + $this->assertEquals($expected, $result); + } + + /** + * Some user cases for validating the expected theme depending on the cohorts, site and user values. + * + * The result is an array of: + * 'User case description' => [ + * 'usertheme' => '', // User theme. + * 'sitetheme' => '', // Site theme. + * 'cohorts' => [], // Cohort themes. + * 'expected' => '', // Expected value returned by cohort_get_user_cohort_theme. + * ] + * + * @return array + */ + public function get_user_theme_provider() { + return [ + 'User not a member of any cohort' => [ + 'usertheme' => '', + 'sitetheme' => 'boost', + 'cohorts' => [], + 'expected' => 'boost', + ], + 'User member of one cohort which has a theme set' => [ + 'usertheme' => '', + 'sitetheme' => 'boost', + 'cohorts' => [ + 'clean', + ], + 'expected' => 'clean', + ], + 'User member of one cohort which has a theme set, and one without a theme' => [ + 'usertheme' => '', + 'sitetheme' => 'boost', + 'cohorts' => [ + 'clean', + '', + ], + 'expected' => 'clean', + ], + 'User member of one cohort which has a theme set, and one with a different theme' => [ + 'usertheme' => '', + 'sitetheme' => 'boost', + 'cohorts' => [ + 'clean', + 'someother', + ], + 'expected' => 'boost', + ], + 'User with a theme but not a member of any cohort' => [ + 'usertheme' => 'more', + 'sitetheme' => 'boost', + 'cohorts' => [], + 'expected' => 'more', + ], + 'User with a theme and member of one cohort which has a theme set' => [ + 'usertheme' => 'more', + 'sitetheme' => 'boost', + 'cohorts' => [ + 'clean', + ], + 'expected' => 'more', + ], + ]; + } } /**