From 29a6589b8dda0b6641a3e58811f7420a6a8561ee Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 20 Sep 2022 08:20:31 +0200 Subject: [PATCH] MDL-75788 testing: Handle new lines in menu profile field generator --- lib/testing/generator/data_generator.php | 5 ++++ lib/testing/tests/testing_generator_test.php | 7 ++++++ .../tests/behat/custom_profile_fields.feature | 24 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index a8727ade1cf31..15e633906178d 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -1315,6 +1315,11 @@ public function create_custom_profile_field(array $data): \stdClass { [$data['categoryid']]) + 1; } + if ($data['datatype'] === 'menu' && isset($data['param1'])) { + // Convert new lines to the proper character. + $data['param1'] = str_replace('\n', "\n", $data['param1']); + } + // Defaults for other values. $defaults = [ 'description' => '', diff --git a/lib/testing/tests/testing_generator_test.php b/lib/testing/tests/testing_generator_test.php index 486886628d37d..28af358bfc6a1 100644 --- a/lib/testing/tests/testing_generator_test.php +++ b/lib/testing/tests/testing_generator_test.php @@ -575,5 +575,12 @@ public function test_create_custom_profile_field() { $field7 = $generator->create_custom_profile_field( ['datatype' => 'checkbox', 'shortname' => 'areyousure', 'name' => 'Are you sure?']); $this->assertEquals(0, $field7->defaultdata); + + // Check setting options for menu using \n work as expected. + $field8 = $generator->create_custom_profile_field([ + 'datatype' => 'menu', 'shortname' => 'cuisine', 'name' => 'Cuisine', 'param1' => 'French\nChinese\nLebanese', + 'defaultdata' => 'Chinese' + ]); + $this->assertEquals("French\nChinese\nLebanese", $field8->param1); } } diff --git a/user/tests/behat/custom_profile_fields.feature b/user/tests/behat/custom_profile_fields.feature index 12080479e6960..abacb27e23b1c 100644 --- a/user/tests/behat/custom_profile_fields.feature +++ b/user/tests/behat/custom_profile_fields.feature @@ -241,3 +241,27 @@ Feature: Custom profile fields should be visible and editable by those with the And I should not see "notvisible_field_information" And I should not see "Edit profile" + + @javascript + Scenario: Menu profile field's default data works as expected when editing user profile + Given the following "custom profile fields" exist: + | datatype | shortname | name | visible | param1 | defaultdata | + | menu | menufield | Menu field | 2 | OptA\nOptB\nOptC | OptB | + And I log in as "userwithinformation" + When I follow "Profile" in the user menu + And I click on "Edit profile" "link" in the "region-main" "region" + Then the following fields match these values: + | Menu field | OptB | + + @javascript + Scenario: Menu profile field successfully updated when editing user profile + Given the following "custom profile fields" exist: + | datatype | shortname | name | visible | param1 | + | menu | menufield | Menu field | 2 | OptA\nOptB\nOptC | + And I log in as "userwithinformation" + When I follow "Profile" in the user menu + And I click on "Edit profile" "link" in the "region-main" "region" + And I set the following fields to these values: + | Menu field | OptC | + And I click on "Update profile" "button" + Then I should see "OptC"