mirrored from git://git.moodle.org/moodle.git
/
behat_course.php
1960 lines (1680 loc) · 82.4 KB
/
behat_course.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Behat course-related steps definitions.
*
* @package core_course
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
use Behat\Gherkin\Node\TableNode as TableNode,
Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Mink\Exception\DriverException as DriverException,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
/**
* Course-related steps definitions.
*
* @package core_course
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_course extends behat_base {
/**
* Turns editing mode on.
* @Given /^I turn editing mode on$/
*/
public function i_turn_editing_mode_on() {
try {
$this->execute("behat_forms::press_button", get_string('turneditingon'));
} catch (Exception $e) {
$this->execute("behat_navigation::i_navigate_to_in_current_page_administration", [get_string('turneditingon')]);
}
}
/**
* Turns editing mode off.
* @Given /^I turn editing mode off$/
*/
public function i_turn_editing_mode_off() {
try {
$this->execute("behat_forms::press_button", get_string('turneditingoff'));
} catch (Exception $e) {
$this->execute("behat_navigation::i_navigate_to_in_current_page_administration", [get_string('turneditingoff')]);
}
}
/**
* Creates a new course with the provided table data matching course settings names with the desired values.
*
* @Given /^I create a course with:$/
* @param TableNode $table The course data
*/
public function i_create_a_course_with(TableNode $table) {
// Go to course management page.
$this->i_go_to_the_courses_management_page();
// Ensure you are on course management page.
$this->execute("behat_course::i_should_see_the_courses_management_page", get_string('categories'));
// Select Miscellaneous category.
$this->i_click_on_category_in_the_management_interface(get_string('miscellaneous'));
$this->execute("behat_course::i_should_see_the_courses_management_page", get_string('categoriesandcoures'));
// Click create new course.
$this->execute('behat_general::i_click_on_in_the',
array(get_string('createnewcourse'), "link", "#course-listing", "css_element")
);
// If the course format is one of the fields we change how we
// fill the form as we need to wait for the form to be set.
$rowshash = $table->getRowsHash();
$formatfieldrefs = array(get_string('format'), 'format', 'id_format');
foreach ($formatfieldrefs as $fieldref) {
if (!empty($rowshash[$fieldref])) {
$formatfield = $fieldref;
}
}
// Setting the format separately.
if (!empty($formatfield)) {
// Removing the format field from the TableNode.
$rows = $table->getRows();
$formatvalue = $rowshash[$formatfield];
foreach ($rows as $key => $row) {
if ($row[0] == $formatfield) {
unset($rows[$key]);
}
}
$table = new TableNode($rows);
// Adding a forced wait until editors are loaded as otherwise selenium sometimes tries clicks on the
// format field when the editor is being rendered and the click misses the field coordinates.
$this->execute("behat_forms::i_expand_all_fieldsets");
$this->execute("behat_forms::i_set_the_field_to", array($formatfield, $formatvalue));
}
// Set form fields.
$this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table);
// Save course settings.
$this->execute("behat_forms::press_button", get_string('savechangesanddisplay'));
}
/**
* Goes to the system courses/categories management page.
*
* @Given /^I go to the courses management page$/
*/
public function i_go_to_the_courses_management_page() {
$parentnodes = get_string('courses', 'admin');
// Go to home page.
$this->execute("behat_general::i_am_on_homepage");
// Navigate to course management via system administration.
$this->execute("behat_navigation::i_navigate_to_in_site_administration",
array($parentnodes . ' > ' . get_string('coursemgmt', 'admin'))
);
}
/**
* Adds the selected activity/resource filling the form data with the specified field/value pairs. Sections 0 and 1 are also allowed on frontpage.
*
* @When /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)" and I fill the form with:$/
* @param string $activity The activity name
* @param int $section The section number
* @param TableNode $data The activity field/value data
*/
public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) {
// Add activity to section.
$this->execute("behat_course::i_add_to_section",
array($this->escape($activity), $this->escape($section))
);
// Wait to be redirected.
$this->execute('behat_general::wait_until_the_page_is_ready');
// Set form fields.
$this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
// Save course settings.
$this->execute("behat_forms::press_button", get_string('savechangesandreturntocourse'));
}
/**
* Opens the activity chooser and opens the activity/resource form page. Sections 0 and 1 are also allowed on frontpage.
*
* @Given /^I add a "(?P<activity_or_resource_name_string>(?:[^"]|\\")*)" to section "(?P<section_number>\d+)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $activity
* @param int $section
*/
public function i_add_to_section($activity, $section) {
if ($this->getSession()->getPage()->find('css', 'body#page-site-index') && (int)$section <= 1) {
// We are on the frontpage.
if ($section) {
// Section 1 represents the contents on the frontpage.
$sectionxpath = "//body[@id='page-site-index']/descendant::div[contains(concat(' ',normalize-space(@class),' '),' sitetopic ')]";
} else {
// Section 0 represents "Site main menu" block.
$sectionxpath = "//div[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]";
}
} else {
// We are inside the course.
$sectionxpath = "//li[@id='section-" . $section . "']";
}
$activityliteral = behat_context_helper::escape(ucfirst($activity));
if ($this->running_javascript()) {
// Clicks add activity or resource section link.
$sectionxpath = $sectionxpath . "/descendant::div[@class='section-modchooser']/span/a";
$sectionnode = $this->find('xpath', $sectionxpath);
$sectionnode->click();
// Clicks the selected activity if it exists.
$activityxpath = "//div[@id='chooseform']/descendant::label" .
"/descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' typename ')]" .
"[normalize-space(.)=$activityliteral]" .
"/parent::label/child::input";
$activitynode = $this->find('xpath', $activityxpath);
$activitynode->doubleClick();
} else {
// Without Javascript.
// Selecting the option from the select box which contains the option.
$selectxpath = $sectionxpath . "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' section_add_menus ')]" .
"/descendant::select[option[normalize-space(.)=$activityliteral]]";
$selectnode = $this->find('xpath', $selectxpath);
$selectnode->selectOption($activity);
// Go button.
$gobuttonxpath = $selectxpath . "/ancestor::form/descendant::input[@type='submit']";
$gobutton = $this->find('xpath', $gobuttonxpath);
$gobutton->click();
}
}
/**
* Opens a section edit menu if it is not already opened.
*
* @Given /^I open section "(?P<section_number>\d+)" edit menu$/
* @throws DriverException The step is not available when Javascript is disabled
* @param string $sectionnumber
*/
public function i_open_section_edit_menu($sectionnumber) {
if (!$this->running_javascript()) {
throw new DriverException('Section edit menu not available when Javascript is disabled');
}
// Wait for section to be available, before clicking on the menu.
$this->i_wait_until_section_is_available($sectionnumber);
// If it is already opened we do nothing.
$xpath = $this->section_exists($sectionnumber);
$xpath .= "/descendant::div[contains(@class, 'section-actions')]/descendant::a[contains(@class, 'textmenu')]";
$exception = new ExpectationException('Section "' . $sectionnumber . '" was not found', $this->getSession());
$menu = $this->find('xpath', $xpath, $exception);
$menu->click();
$this->i_wait_until_section_is_available($sectionnumber);
}
/**
* Deletes course section.
*
* @Given /^I delete section "(?P<section_number>\d+)"$/
* @param int $sectionnumber The section number
*/
public function i_delete_section($sectionnumber) {
// Ensures the section exists.
$xpath = $this->section_exists($sectionnumber);
// We need to know the course format as the text strings depends on them.
$courseformat = $this->get_course_format();
if (get_string_manager()->string_exists('deletesection', $courseformat)) {
$strdelete = get_string('deletesection', $courseformat);
} else {
$strdelete = get_string('deletesection');
}
// If javascript is on, link is inside a menu.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
// Click on delete link.
$this->execute('behat_general::i_click_on_in_the',
array($strdelete, "link", $this->escape($xpath), "xpath_element")
);
}
/**
* Turns course section highlighting on.
*
* @Given /^I turn section "(?P<section_number>\d+)" highlighting on$/
* @param int $sectionnumber The section number
*/
public function i_turn_section_highlighting_on($sectionnumber) {
// Ensures the section exists.
$xpath = $this->section_exists($sectionnumber);
// If javascript is on, link is inside a menu.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
// Click on highlight topic link.
$this->execute('behat_general::i_click_on_in_the',
array(get_string('markthistopic'), "link", $this->escape($xpath), "xpath_element")
);
}
/**
* Turns course section highlighting off.
*
* @Given /^I turn section "(?P<section_number>\d+)" highlighting off$/
* @param int $sectionnumber The section number
*/
public function i_turn_section_highlighting_off($sectionnumber) {
// Ensures the section exists.
$xpath = $this->section_exists($sectionnumber);
// If javascript is on, link is inside a menu.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
// Click on un-highlight topic link.
$this->execute('behat_general::i_click_on_in_the',
array(get_string('markedthistopic'), "link", $this->escape($xpath), "xpath_element")
);
}
/**
* Shows the specified hidden section. You need to be in the course page and on editing mode.
*
* @Given /^I show section "(?P<section_number>\d+)"$/
* @param int $sectionnumber
*/
public function i_show_section($sectionnumber) {
$showlink = $this->show_section_link_exists($sectionnumber);
// Ensure section edit menu is open before interacting with it.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
$showlink->click();
if ($this->running_javascript()) {
$this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
$this->i_wait_until_section_is_available($sectionnumber);
}
}
/**
* Hides the specified visible section. You need to be in the course page and on editing mode.
*
* @Given /^I hide section "(?P<section_number>\d+)"$/
* @param int $sectionnumber
*/
public function i_hide_section($sectionnumber) {
// Ensures the section exists.
$xpath = $this->section_exists($sectionnumber);
// We need to know the course format as the text strings depends on them.
$courseformat = $this->get_course_format();
if (get_string_manager()->string_exists('hidefromothers', $courseformat)) {
$strhide = get_string('hidefromothers', $courseformat);
} else {
$strhide = get_string('hidesection');
}
// If javascript is on, link is inside a menu.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
// Click on delete link.
$this->execute('behat_general::i_click_on_in_the',
array($strhide, "link", $this->escape($xpath), "xpath_element")
);
if ($this->running_javascript()) {
$this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
$this->i_wait_until_section_is_available($sectionnumber);
}
}
/**
* Go to editing section page for specified section number. You need to be in the course page and on editing mode.
*
* @Given /^I edit the section "(?P<section_number>\d+)"$/
* @param int $sectionnumber
*/
public function i_edit_the_section($sectionnumber) {
// If javascript is on, link is inside a menu.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
// We need to know the course format as the text strings depends on them.
$courseformat = $this->get_course_format();
if ($sectionnumber > 0 && get_string_manager()->string_exists('editsection', $courseformat)) {
$stredit = get_string('editsection', $courseformat);
} else {
$stredit = get_string('editsection');
}
// Click on un-highlight topic link.
$this->execute('behat_general::i_click_on_in_the',
array($stredit, "link", "#section-" . $sectionnumber, "css_element")
);
}
/**
* Edit specified section and fill the form data with the specified field/value pairs.
*
* @When /^I edit the section "(?P<section_number>\d+)" and I fill the form with:$/
* @param int $sectionnumber The section number
* @param TableNode $data The activity field/value data
*/
public function i_edit_the_section_and_i_fill_the_form_with($sectionnumber, TableNode $data) {
// Edit given section.
$this->execute("behat_course::i_edit_the_section", $sectionnumber);
// Set form fields.
$this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
// Save section settings.
$this->execute("behat_forms::press_button", get_string('savechanges'));
}
/**
* Checks if the specified course section hightlighting is turned on. You need to be in the course page on editing mode.
*
* @Then /^section "(?P<section_number>\d+)" should be highlighted$/
* @throws ExpectationException
* @param int $sectionnumber The section number
*/
public function section_should_be_highlighted($sectionnumber) {
// Ensures the section exists.
$xpath = $this->section_exists($sectionnumber);
// The important checking, we can not check the img.
$this->execute('behat_general::should_exist_in_the', ['This topic is highlighted as the current topic', 'icon', $xpath, 'xpath_element']);
}
/**
* Checks if the specified course section highlighting is turned off. You need to be in the course page on editing mode.
*
* @Then /^section "(?P<section_number>\d+)" should not be highlighted$/
* @throws ExpectationException
* @param int $sectionnumber The section number
*/
public function section_should_not_be_highlighted($sectionnumber) {
// We only catch ExpectationException, ElementNotFoundException should be thrown if the specified section does not exist.
try {
$this->section_should_be_highlighted($sectionnumber);
} catch (ExpectationException $e) {
// ExpectedException means that it is not highlighted.
return;
}
throw new ExpectationException('The "' . $sectionnumber . '" section is highlighted', $this->getSession());
}
/**
* Checks that the specified section is visible. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
*
* @Then /^section "(?P<section_number>\d+)" should be hidden$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param int $sectionnumber
*/
public function section_should_be_hidden($sectionnumber) {
$sectionxpath = $this->section_exists($sectionnumber);
// Preventive in case there is any action in progress.
// Adding it here because we are interacting (click) with
// the elements, not necessary when we just find().
$this->i_wait_until_section_is_available($sectionnumber);
// Section should be hidden.
$exception = new ExpectationException('The section is not hidden', $this->getSession());
$this->find('xpath', $sectionxpath . "[contains(concat(' ', normalize-space(@class), ' '), ' hidden ')]", $exception);
}
/**
* Checks that all actiities in the specified section are hidden. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
*
* @Then /^all activities in section "(?P<section_number>\d+)" should be hidden$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param int $sectionnumber
*/
public function section_activities_should_be_hidden($sectionnumber) {
$sectionxpath = $this->section_exists($sectionnumber);
// Preventive in case there is any action in progress.
// Adding it here because we are interacting (click) with
// the elements, not necessary when we just find().
$this->i_wait_until_section_is_available($sectionnumber);
// The checking are different depending on user permissions.
if ($this->is_course_editor()) {
// The section must be hidden.
$this->show_section_link_exists($sectionnumber);
// If there are activities they should be hidden and the visibility icon should not be available.
if ($activities = $this->get_section_activities($sectionxpath)) {
$dimmedexception = new ExpectationException('There are activities that are not dimmed', $this->getSession());
foreach ($activities as $activity) {
// Dimmed.
$this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' activityinstance ')]" .
"//a[contains(concat(' ', normalize-space(@class), ' '), ' dimmed ')]", $dimmedexception, $activity);
}
}
} else {
// There shouldn't be activities.
if ($this->get_section_activities($sectionxpath)) {
throw new ExpectationException('There are activities in the section and they should be hidden', $this->getSession());
}
}
}
/**
* Checks that the specified section is visible. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
*
* @Then /^section "(?P<section_number>\d+)" should be visible$/
* @throws ExpectationException
* @param int $sectionnumber
*/
public function section_should_be_visible($sectionnumber) {
$sectionxpath = $this->section_exists($sectionnumber);
// Section should not be hidden.
$xpath = $sectionxpath . "[not(contains(concat(' ', normalize-space(@class), ' '), ' hidden '))]";
if (!$this->getSession()->getPage()->find('xpath', $xpath)) {
throw new ExpectationException('The section is hidden', $this->getSession());
}
// Edit menu should be visible.
if ($this->is_course_editor()) {
$xpath = $sectionxpath .
"/descendant::div[contains(@class, 'section-actions')]" .
"/descendant::a[contains(@class, 'textmenu')]";
if (!$this->getSession()->getPage()->find('xpath', $xpath)) {
throw new ExpectationException('The section edit menu is not available', $this->getSession());
}
}
}
/**
* Moves up the specified section, this step only works with Javascript disabled. Editing mode should be on.
*
* @Given /^I move up section "(?P<section_number>\d+)"$/
* @throws DriverException Step not available when Javascript is enabled
* @param int $sectionnumber
*/
public function i_move_up_section($sectionnumber) {
if ($this->running_javascript()) {
throw new DriverException('Move a section up step is not available with Javascript enabled');
}
// Ensures the section exists.
$sectionxpath = $this->section_exists($sectionnumber);
// If javascript is on, link is inside a menu.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
// Follows the link
$moveuplink = $this->get_node_in_container('link', get_string('moveup'), 'xpath_element', $sectionxpath);
$moveuplink->click();
}
/**
* Moves down the specified section, this step only works with Javascript disabled. Editing mode should be on.
*
* @Given /^I move down section "(?P<section_number>\d+)"$/
* @throws DriverException Step not available when Javascript is enabled
* @param int $sectionnumber
*/
public function i_move_down_section($sectionnumber) {
if ($this->running_javascript()) {
throw new DriverException('Move a section down step is not available with Javascript enabled');
}
// Ensures the section exists.
$sectionxpath = $this->section_exists($sectionnumber);
// If javascript is on, link is inside a menu.
if ($this->running_javascript()) {
$this->i_open_section_edit_menu($sectionnumber);
}
// Follows the link
$movedownlink = $this->get_node_in_container('link', get_string('movedown'), 'xpath_element', $sectionxpath);
$movedownlink->click();
}
/**
* Checks that the specified activity is visible. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
*
* @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be visible$/
* @param string $activityname
* @throws ExpectationException
*/
public function activity_should_be_visible($activityname) {
// The activity must exists and be visible.
$activitynode = $this->get_activity_node($activityname);
if ($this->is_course_editor()) {
// The activity should not be dimmed.
try {
$xpath = "/descendant-or-self::a[contains(concat(' ', normalize-space(@class), ' '), ' dimmed ')] | ".
"/descendant-or-self::div[contains(concat(' ', normalize-space(@class), ' '), ' dimmed_text ')]";
$this->find('xpath', $xpath, false, $activitynode);
throw new ExpectationException('"' . $activityname . '" is hidden', $this->getSession());
} catch (ElementNotFoundException $e) {
// All ok.
}
// Additional check if this is a teacher in editing mode.
if ($this->is_editing_on()) {
// The 'Hide' button should be available.
$nohideexception = new ExpectationException('"' . $activityname . '" doesn\'t have a "' .
get_string('hide') . '" icon', $this->getSession());
$this->find('named_partial', array('link', get_string('hide')), $nohideexception, $activitynode);
}
}
}
/**
* Checks that the specified activity is visible. You need to be in the course page.
* It can be used being logged as a student and as a teacher on editing mode.
*
* @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be available but hidden from course page$/
* @param string $activityname
* @throws ExpectationException
*/
public function activity_should_be_available_but_hidden_from_course_page($activityname) {
if ($this->is_course_editor()) {
// The activity must exists and be visible.
$activitynode = $this->get_activity_node($activityname);
// The activity should not be dimmed.
try {
$xpath = "/descendant-or-self::a[contains(concat(' ', normalize-space(@class), ' '), ' dimmed ')] | " .
"/descendant-or-self::div[contains(concat(' ', normalize-space(@class), ' '), ' dimmed_text ')]";
$this->find('xpath', $xpath, false, $activitynode);
throw new ExpectationException('"' . $activityname . '" is hidden', $this->getSession());
} catch (ElementNotFoundException $e) {
// All ok.
}
// Should has "stealth" class.
$exception = new ExpectationException('"' . $activityname . '" does not have CSS class "stealth"', $this->getSession());
$xpath = "/descendant-or-self::a[contains(concat(' ', normalize-space(@class), ' '), ' stealth ')]";
$this->find('xpath', $xpath, $exception, $activitynode);
// Additional check if this is a teacher in editing mode.
if ($this->is_editing_on()) {
// Also has either 'Hide' or 'Make unavailable' edit control.
$nohideexception = new ExpectationException('"' . $activityname . '" has neither "' . get_string('hide') .
'" nor "' . get_string('makeunavailable') . '" icons', $this->getSession());
try {
$this->find('named_partial', array('link', get_string('hide')), false, $activitynode);
} catch (ElementNotFoundException $e) {
$this->find('named_partial', array('link', get_string('makeunavailable')), $nohideexception, $activitynode);
}
}
} else {
// Student should not see the activity at all.
try {
$this->get_activity_node($activityname);
throw new ExpectationException('The "' . $activityname . '" should not appear', $this->getSession());
} catch (ElementNotFoundException $e) {
// This is good, the activity should not be there.
}
}
}
/**
* Checks that the specified activity is hidden. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
*
* @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be hidden$/
* @param string $activityname
* @throws ExpectationException
*/
public function activity_should_be_hidden($activityname) {
if ($this->is_course_editor()) {
// The activity should exist.
$activitynode = $this->get_activity_node($activityname);
// Should be hidden.
$exception = new ExpectationException('"' . $activityname . '" is not dimmed', $this->getSession());
$xpath = "/descendant-or-self::a[contains(concat(' ', normalize-space(@class), ' '), ' dimmed ')] | ".
"/descendant-or-self::div[contains(concat(' ', normalize-space(@class), ' '), ' dimmed_text ')]";
$this->find('xpath', $xpath, $exception, $activitynode);
// Additional check if this is a teacher in editing mode.
if ($this->is_editing_on()) {
// Also has either 'Show' or 'Make available' edit control.
$noshowexception = new ExpectationException('"' . $activityname . '" has neither "' . get_string('show') .
'" nor "' . get_string('makeavailable') . '" icons', $this->getSession());
try {
$this->find('named_partial', array('link', get_string('show')), false, $activitynode);
} catch (ElementNotFoundException $e) {
$this->find('named_partial', array('link', get_string('makeavailable')), $noshowexception, $activitynode);
}
}
} else {
// It should not exist at all.
try {
$this->get_activity_node($activityname);
throw new ExpectationException('The "' . $activityname . '" should not appear', $this->getSession());
} catch (ElementNotFoundException $e) {
// This is good, the activity should not be there.
}
}
}
/**
* Checks that the specified activity is dimmed. You need to be in the course page.
*
* @Then /^"(?P<activity_or_resource_string>(?:[^"]|\\")*)" activity should be dimmed$/
* @param string $activityname
* @throws ExpectationException
*/
public function activity_should_be_dimmed($activityname) {
// The activity should exist.
$activitynode = $this->get_activity_node($activityname);
// Should be hidden.
$exception = new ExpectationException('"' . $activityname . '" is not dimmed', $this->getSession());
$xpath = "/descendant-or-self::a[contains(concat(' ', normalize-space(@class), ' '), ' dimmed ')] | ".
"/descendant-or-self::div[contains(concat(' ', normalize-space(@class), ' '), ' dimmed_text ')]";
$this->find('xpath', $xpath, $exception, $activitynode);
}
/**
* Moves the specified activity to the first slot of a section. This step is experimental when using it in Javascript tests. Editing mode should be on.
*
* @Given /^I move "(?P<activity_name_string>(?:[^"]|\\")*)" activity to section "(?P<section_number>\d+)"$/
* @param string $activityname The activity name
* @param int $sectionnumber The number of section
*/
public function i_move_activity_to_section($activityname, $sectionnumber) {
// Ensure the destination is valid.
$sectionxpath = $this->section_exists($sectionnumber);
$activitynode = $this->get_activity_element('Move', 'icon', $activityname);
// JS enabled.
if ($this->running_javascript()) {
$destinationxpath = $sectionxpath . "/descendant::ul[contains(concat(' ', normalize-space(@class), ' '), ' yui3-dd-drop ')]";
$this->execute("behat_general::i_drag_and_i_drop_it_in",
array($this->escape($activitynode->getXpath()), "xpath_element",
$this->escape($destinationxpath), "xpath_element")
);
} else {
// Following links with no-JS.
// Moving to the fist spot of the section (before all other section's activities).
$this->execute('behat_course::i_click_on_in_the_activity',
array("a.editing_move", "css_element", $this->escape($activityname))
);
$this->execute('behat_general::i_click_on_in_the',
array("li.movehere a", "css_element", $this->escape($sectionxpath), "xpath_element")
);
}
}
/**
* Edits the activity name through the edit activity; this step only works with Javascript enabled. Editing mode should be on.
*
* @Given /^I change "(?P<activity_name_string>(?:[^"]|\\")*)" activity name to "(?P<new_name_string>(?:[^"]|\\")*)"$/
* @throws DriverException Step not available when Javascript is disabled
* @param string $activityname
* @param string $newactivityname
*/
public function i_change_activity_name_to($activityname, $newactivityname) {
if (!$this->running_javascript()) {
throw new DriverException('Change activity name step is not available with Javascript disabled');
}
$activity = $this->escape($activityname);
$this->execute('behat_course::i_click_on_in_the_activity',
array(get_string('edittitle'), "link", $activity)
);
// Adding chr(10) to save changes.
$this->execute('behat_forms::i_set_the_field_to',
array('title', $this->escape($newactivityname) . chr(10))
);
}
/**
* Opens an activity actions menu if it is not already opened.
*
* @Given /^I open "(?P<activity_name_string>(?:[^"]|\\")*)" actions menu$/
* @throws DriverException The step is not available when Javascript is disabled
* @param string $activityname
*/
public function i_open_actions_menu($activityname) {
if (!$this->running_javascript()) {
throw new DriverException('Activities actions menu not available when Javascript is disabled');
}
// If it is already opened we do nothing.
$activitynode = $this->get_activity_node($activityname);
$classes = array_flip(explode(' ', $activitynode->getAttribute('class')));
if (!empty($classes['action-menu-shown'])) {
return;
}
$this->execute('behat_course::i_click_on_in_the_activity',
array("a[role='menuitem']", "css_element", $this->escape($activityname))
);
}
/**
* Closes an activity actions menu if it is not already closed.
*
* @Given /^I close "(?P<activity_name_string>(?:[^"]|\\")*)" actions menu$/
* @throws DriverException The step is not available when Javascript is disabled
* @param string $activityname
*/
public function i_close_actions_menu($activityname) {
if (!$this->running_javascript()) {
throw new DriverException('Activities actions menu not available when Javascript is disabled');
}
// If it is already closed we do nothing.
$activitynode = $this->get_activity_node($activityname);
$classes = array_flip(explode(' ', $activitynode->getAttribute('class')));
if (empty($classes['action-menu-shown'])) {
return;
}
$this->execute('behat_course::i_click_on_in_the_activity',
array("a[role='menuitem']", "css_element", $this->escape($activityname))
);
}
/**
* Checks that the specified activity's action menu is open.
*
* @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" actions menu should be open$/
* @throws DriverException The step is not available when Javascript is disabled
* @param string $activityname
*/
public function actions_menu_should_be_open($activityname) {
if (!$this->running_javascript()) {
throw new DriverException('Activities actions menu not available when Javascript is disabled');
}
// If it is already closed we do nothing.
$activitynode = $this->get_activity_node($activityname);
$classes = array_flip(explode(' ', $activitynode->getAttribute('class')));
if (empty($classes['action-menu-shown'])) {
throw new ExpectationException(sprintf("The action menu for '%s' is not open", $activityname), $this->getSession());
}
}
/**
* Checks that the specified activity's action menu contains an item.
*
* @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" actions menu should have "(?P<menu_item_string>(?:[^"]|\\")*)" item$/
* @throws DriverException The step is not available when Javascript is disabled
* @param string $activityname
* @param string $menuitem
*/
public function actions_menu_should_have_item($activityname, $menuitem) {
$activitynode = $this->get_activity_node($activityname);
$notfoundexception = new ExpectationException('"' . $activityname . '" doesn\'t have a "' .
$menuitem . '" item', $this->getSession());
$this->find('named_partial', array('link', $menuitem), $notfoundexception, $activitynode);
}
/**
* Checks that the specified activity's action menu does not contains an item.
*
* @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" actions menu should not have "(?P<menu_item_string>(?:[^"]|\\")*)" item$/
* @throws DriverException The step is not available when Javascript is disabled
* @param string $activityname
* @param string $menuitem
*/
public function actions_menu_should_not_have_item($activityname, $menuitem) {
$activitynode = $this->get_activity_node($activityname);
try {
$this->find('named_partial', array('link', $menuitem), false, $activitynode);
throw new ExpectationException('"' . $activityname . '" has a "' . $menuitem .
'" item when it should not', $this->getSession());
} catch (ElementNotFoundException $e) {
// This is good, the menu item should not be there.
}
}
/**
* Indents to the right the activity or resource specified by it's name. Editing mode should be on.
*
* @Given /^I indent right "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
* @param string $activityname
*/
public function i_indent_right_activity($activityname) {
$activity = $this->escape($activityname);
if ($this->running_javascript()) {
$this->i_open_actions_menu($activity);
}
$this->execute('behat_course::i_click_on_in_the_activity',
array(get_string('moveright'), "link", $this->escape($activity))
);
}
/**
* Indents to the left the activity or resource specified by it's name. Editing mode should be on.
*
* @Given /^I indent left "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
* @param string $activityname
*/
public function i_indent_left_activity($activityname) {
$activity = $this->escape($activityname);
if ($this->running_javascript()) {
$this->i_open_actions_menu($activity);
}
$this->execute('behat_course::i_click_on_in_the_activity',
array(get_string('moveleft'), "link", $this->escape($activity))
);
}
/**
* Deletes the activity or resource specified by it's name. This step is experimental when using it in Javascript tests. You should be in the course page with editing mode on.
*
* @Given /^I delete "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
* @param string $activityname
*/
public function i_delete_activity($activityname) {
$steps = array();
$activity = $this->escape($activityname);
if ($this->running_javascript()) {
$this->i_open_actions_menu($activity);
}
$this->execute('behat_course::i_click_on_in_the_activity',
array(get_string('delete'), "link", $this->escape($activity))
);
// JS enabled.
// Not using chain steps here because the exceptions catcher have problems detecting
// JS modal windows and avoiding interacting them at the same time.
if ($this->running_javascript()) {
$this->execute('behat_general::i_click_on_in_the',
array(get_string('yes'), "button", "Confirm", "dialogue")
);