@@ -366,12 +366,15 @@ protected function runtime_js(string $script) {
366366 *
367367 * @param string $script
368368 * @param bool $blocking
369+ * @param string $texttofind If set, when this text is found the operation is considered finished. This is useful for
370+ * operations that might expect user input before finishing, like a confirm modal.
369371 * @return mixed Result.
370372 */
371- protected function zone_js (string $ script , bool $ blocking = false ) {
373+ protected function zone_js (string $ script , bool $ blocking = false , string $ texttofind = '' ) {
372374 $ blockingjson = json_encode ($ blocking );
375+ $ locatortofind = !empty ($ texttofind ) ? json_encode ((object ) ['text ' => $ texttofind ]) : null ;
373376
374- return $ this ->runtime_js ("runInZone(() => window.behat. $ script, $ blockingjson) " );
377+ return $ this ->runtime_js ("runInZone(() => window.behat. $ script, $ blockingjson, $ locatortofind ) " );
375378 }
376379
377380 /**
@@ -411,16 +414,14 @@ protected function open_moodleapp_custom_login_url($username, $path = '', string
411414 $ privatetoken = $ usertoken ->privatetoken ;
412415 }
413416
414- // Generate custom URL.
415- $ parsed_url = parse_url ( $ CFG -> behat_wwwroot );
416- $ site = $ parsed_url [ ' host ' ] ?? '' ;
417- $ site .= isset ( $ parsed_url [ ' port ' ]) ? ' : ' . $ parsed_url [ ' port ' ] : '' ;
418- $ site .= $ parsed_url [ ' path ' ] ?? '' ;
419- $ url = $ this -> get_mobile_url_scheme () . " :// $ username @ $ site ?token= $ token &privatetoken= $ privatetoken " ;
417+ $ url = $ this -> generate_custom_url ([
418+ ' username ' => $ username ,
419+ ' token ' => $ token ,
420+ ' privatetoken ' => $ privatetoken ,
421+ ' redirect ' => $ path ,
422+ ]) ;
420423
421- if (!empty ($ path )) {
422- $ url .= '&redirect= ' .urlencode ($ CFG ->behat_wwwroot .$ path );
423- } else {
424+ if (empty ($ path )) {
424425 $ successXPath = '//page-core-mainmenu ' ;
425426 }
426427
@@ -434,24 +435,66 @@ protected function open_moodleapp_custom_login_url($username, $path = '', string
434435 *
435436 * @param string $path To navigate.
436437 * @param string $successXPath The XPath of the element to lookat after navigation.
438+ * @param string $username The username to use.
439+ */
440+ protected function open_moodleapp_custom_url (string $ path , string $ successXPath = '' , string $ username = '' ) {
441+ global $ CFG ;
442+
443+ $ url = $ this ->generate_custom_url ([
444+ 'username ' => $ username ,
445+ 'redirect ' => $ path ,
446+ ]);
447+
448+ $ this ->handle_url ($ url , $ successXPath , $ username ? 'This link belongs to another site ' : '' );
449+ }
450+
451+ /**
452+ * Generates a custom URL to be treated by the app.
453+ *
454+ * @param array $data Data to generate the URL.
437455 */
438- protected function open_moodleapp_custom_url ( string $ path , string $ successXPath = '' ) {
456+ protected function generate_custom_url ( array $ data ): string {
439457 global $ CFG ;
440458
441- $ urlscheme = $ this ->get_mobile_url_scheme ();
442- $ url = "$ urlscheme://link= " . urlencode ($ CFG ->behat_wwwroot .$ path );
459+ $ parsed_url = parse_url ($ CFG ->behat_wwwroot );
460+ $ parameters = [];
461+
462+ $ url = $ this ->get_mobile_url_scheme () . ':// ' . ($ parsed_url ['scheme ' ] ?? 'http ' ) . ':// ' ;
463+ if (!empty ($ data ['username ' ])) {
464+ $ url .= $ data ['username ' ] . '@ ' ;
465+ }
466+ $ url .= $ parsed_url ['host ' ] ?? '' ;
467+ $ url .= isset ($ parsed_url ['port ' ]) ? ': ' . $ parsed_url ['port ' ] : '' ;
468+ $ url .= $ parsed_url ['path ' ] ?? '' ;
469+
470+ if (!empty ($ data ['token ' ])) {
471+ $ parameters [] = 'token= ' . $ data ['token ' ];
472+ if (!empty ($ data ['privatetoken ' ])) {
473+ $ parameters [] = 'privatetoken= ' . $ data ['privatetoken ' ];
474+ }
475+ }
476+
477+ if (!empty ($ data ['redirect ' ])) {
478+ $ parameters [] = 'redirect= ' . urlencode ($ data ['redirect ' ]);
479+ }
480+
481+ if (!empty ($ parameters )) {
482+ $ url .= '? ' . implode ('& ' , $ parameters );
483+ }
443484
444- $ this -> handle_url ( $ url) ;
485+ return $ url ;
445486 }
446487
447488 /**
448489 * Handles the custom URL on the Moodle App (and waits to finish.)
449490 *
450491 * @param string $customurl To navigate.
451492 * @param string $successXPath The XPath of the element to lookat after navigation.
493+ * @param string $texttofind If set, when this text is found the operation is considered finished. This is useful for
494+ * operations that might expect user input before finishing, like a confirm modal.
452495 */
453- protected function handle_url (string $ customurl , string $ successXPath = '' ) {
454- $ result = $ this ->zone_js ("customUrlSchemes.handleCustomURL(' $ customurl') " );
496+ protected function handle_url (string $ customurl , string $ successXPath = '' , string $ texttofind = '' ) {
497+ $ result = $ this ->zone_js ("customUrlSchemes.handleCustomURL(' $ customurl') " , false , $ texttofind );
455498
456499 if ($ result !== 'OK ' ) {
457500 throw new DriverException ('Error handling url - ' . $ customurl . ' - ' .$ result );
0 commit comments