forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_helper_spec.rb
776 lines (654 loc) · 30.7 KB
/
application_helper_spec.rb
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
# coding: utf-8
#
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas 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 Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'nokogiri'
describe ApplicationHelper do
include ERB::Util
alias_method :content_tag_without_nil_return, :content_tag
context "folders_as_options" do
before(:once) do
course_model
@f = Folder.create!(:name => 'f', :context => @course)
@f_1 = Folder.create!(:name => 'f_1', :parent_folder => @f, :context => @course)
@f_2 = Folder.create!(:name => 'f_2', :parent_folder => @f, :context => @course)
@f_2_1 = Folder.create!(:name => 'f_2_1', :parent_folder => @f_2, :context => @course)
@f_2_1_1 = Folder.create!(:name => 'f_2_1_1', :parent_folder => @f_2_1, :context => @course)
@all_folders = [ @f, @f_1, @f_2, @f_2_1, @f_2_1_1 ]
end
it "should work work recursively" do
option_string = folders_as_options([@f], :all_folders => @all_folders)
html = Nokogiri::HTML::DocumentFragment.parse("<select>#{option_string}</select>")
expect(html.css('option').count).to eq 5
expect(html.css('option')[0].text).to eq @f.name
expect(html.css('option')[1].text).to match /^\xC2\xA0\xC2\xA0\xC2\xA0- #{@f_1.name}/
expect(html.css('option')[4].text).to match /^\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0- #{@f_2_1_1.name}/
end
it "should limit depth" do
option_string = folders_as_options([@f], :all_folders => @all_folders, :max_depth => 1)
html = Nokogiri::HTML::DocumentFragment.parse("<select>#{option_string}</select>")
expect(html.css('option').count).to eq 3
expect(html.css('option')[0].text).to eq @f.name
expect(html.css('option')[1].text).to match /^\xC2\xA0\xC2\xA0\xC2\xA0- #{@f_1.name}/
expect(html.css('option')[2].text).to match /^\xC2\xA0\xC2\xA0\xC2\xA0- #{@f_2.name}/
end
it "should work without supplying all folders" do
option_string = folders_as_options([@f])
html = Nokogiri::HTML::DocumentFragment.parse("<select>#{option_string}</select>")
expect(html.css('option').count).to eq 5
expect(html.css('option')[0].text).to eq @f.name
expect(html.css('option')[1].text).to match /^\xC2\xA0\xC2\xA0\xC2\xA0- #{@f_1.name}/
expect(html.css('option')[4].text).to match /^\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0- #{@f_2_1_1.name}/
end
end
it "show_user_create_course_button should work" do
Account.default.update_attribute(:settings, { :teachers_can_create_courses => true, :students_can_create_courses => true })
@domain_root_account = Account.default
expect(show_user_create_course_button(nil)).to be_falsey
user_factory
expect(show_user_create_course_button(@user)).to be_falsey
course_with_teacher
expect(show_user_create_course_button(@teacher)).to be_truthy
account_admin_user
expect(show_user_create_course_button(@admin)).to be_truthy
end
describe "tomorrow_at_midnight" do
it "should always return a time in the future" do
now = 1.day.from_now.midnight - 5.seconds
expect(tomorrow_at_midnight).to be > now
end
end
describe "Time Display Helpers" do
before :each do
@zone = Time.zone
Time.zone = "Alaska"
end
after :each do
Time.zone = @zone
end
around do |example|
Timecop.freeze(Time.zone.local(2013,3,13,9,12), &example)
end
describe '#context_sensitive_datetime_title' do
it "produces a string showing the local time and the course time" do
context = double(time_zone: ActiveSupport::TimeZone["America/Denver"])
expect(context_sensitive_datetime_title(Time.now, context)).to eq "data-tooltip data-html-tooltip-title=\"Local: Mar 13 at 1:12am<br>Course: Mar 13 at 3:12am\""
end
it "only prints the text if just_text option passed" do
context = double(time_zone: ActiveSupport::TimeZone["America/Denver"])
expect(context_sensitive_datetime_title(Time.now, context, just_text: true)).to eq "Local: Mar 13 at 1:12am<br>Course: Mar 13 at 3:12am"
end
it "uses the simple title if theres no timezone difference" do
context = double(time_zone: ActiveSupport::TimeZone["America/Anchorage"])
expect(context_sensitive_datetime_title(Time.now, context, just_text: true)).to eq "Mar 13 at 1:12am"
expect(context_sensitive_datetime_title(Time.now, context)).to eq "data-tooltip data-html-tooltip-title=\"Mar 13 at 1:12am\""
end
it 'uses the simple title for nil context' do
expect(context_sensitive_datetime_title(Time.now, nil, just_text: true)).to eq "Mar 13 at 1:12am"
end
it 'crosses date boundaries appropriately' do
Timecop.freeze(Time.utc(2013,3,13,7,12)) do
context = double(time_zone: ActiveSupport::TimeZone["America/Denver"])
expect(context_sensitive_datetime_title(Time.now, context)).to eq "data-tooltip data-html-tooltip-title=\"Local: Mar 12 at 11:12pm<br>Course: Mar 13 at 1:12am\""
end
end
end
describe '#friendly_datetime' do
let(:context) { double(time_zone: ActiveSupport::TimeZone["America/Denver"]) }
it 'spits out a friendly time tag' do
tag = friendly_datetime(Time.now)
expect(tag).to eq "<time data-html-tooltip-title=\"Mar 13 at 1:12am\" data-tooltip=\"top\">Mar 13 at 1:12am</time>"
end
it 'builds a whole time tag with a useful title showing the timezone offset if theres a context' do
tag = friendly_datetime(Time.now, context: context)
expect(tag).to match /^<time.*<\/time>$/
expect(tag).to match /data-html-tooltip-title=/
expect(tag).to match /Local: Mar 13 at 1:12am/
expect(tag).to match /Course: Mar 13 at 3:12am/
end
it 'can produce an alternate tag type' do
tag = friendly_datetime(Time.now, context: context, tag_type: :span)
expect(tag).to match /^<span.*<\/span>$/
expect(tag).to match /data-html-tooltip-title=/
expect(tag).to match /Local: Mar 13 at 1:12am/
expect(tag).to match /Course: Mar 13 at 3:12am/
end
it 'produces no tooltip for a nil datetime' do
tag = friendly_datetime(nil, context: context)
expect(tag).to eq "<time></time>"
end
end
end
describe "accessible date formats" do
it "generates a date format for use throughout the app" do
expect(accessible_date_format).to match(/YYYY/)
expect(accessible_date_format).to match(/hh:mm/)
end
it "wraps a prompt around the format for Screenreader users" do
expect(datepicker_screenreader_prompt).to include(accessible_date_format)
end
it "produces a date-only format" do
format = accessible_date_format('date')
expect(format).to match(/YYYY/)
expect(format).to_not match(/hh:mm/)
end
it "produces a time-only format" do
format = accessible_date_format('time')
expect(format).to_not match(/YYYY/)
expect(format).to match(/hh:mm/)
end
it "throws an argument error for a foolish format" do
expect{ accessible_date_format('nonsense') }.to raise_error(ArgumentError)
end
end
describe "custom css/js includes" do
def set_up_subaccounts
@domain_root_account.settings[:global_includes] = true
@domain_root_account.settings[:sub_account_includes] = true
@domain_root_account.create_brand_config!({
css_overrides: 'https://example.com/root/account.css',
js_overrides: 'https://example.com/root/account.js'
})
@domain_root_account.save!
@child_account = account_model(root_account: @domain_root_account, name: 'child account')
bc = @child_account.build_brand_config({
css_overrides: 'https://example.com/child/account.css',
js_overrides: 'https://example.com/child/account.js'
})
bc.parent = @domain_root_account.brand_config
bc.save!
@child_account.save!
@grandchild_account = @child_account.sub_accounts.create!(name: 'grandchild account')
bc = @grandchild_account.build_brand_config({
css_overrides: 'https://example.com/grandchild/account.css',
js_overrides: 'https://example.com/grandchild/account.js'
})
bc.parent = @child_account.brand_config
bc.save!
@grandchild_account.save!
end
describe "include_account_css" do
before :once do
@site_admin = Account.site_admin
@domain_root_account = Account.default
@domain_root_account.settings = @domain_root_account.settings.merge(global_includes: true)
@domain_root_account.save!
end
context "with no custom css" do
it "should be empty" do
allow(helper).to receive(:active_brand_config).and_return(nil)
expect(helper.include_account_css).to be_nil
end
end
context "with custom css" do
it "should include account css" do
allow(helper).to receive(:active_brand_config).and_return BrandConfig.create!(css_overrides: 'https://example.com/path/to/overrides.css')
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output).to match %r{https://example.com/path/to/overrides.css}
end
it "should include site_admin css even if there is no active brand" do
allow(helper).to receive(:active_brand_config).and_return nil
Account.site_admin.create_brand_config!({
css_overrides: 'https://example.com/site_admin/account.css',
js_overrides: 'https://example.com/site_admin/account.js'
})
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output).to match %r{https://example.com/site_admin/account.css}
end
it "should not include anything if param is set to 0" do
allow(helper).to receive(:active_brand_config).and_return BrandConfig.create!(css_overrides: 'https://example.com/path/to/overrides.css')
params[:global_includes] = '0'
output = helper.include_account_css
expect(output).to be_nil
end
end
context "sub-accounts" do
before { set_up_subaccounts }
it "should include sub-account css when viewing the subaccount or any course or group in it" do
course = @grandchild_account.courses.create!
group = course.groups.create!
[@grandchild_account, course, group].each do |context|
@context = context
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output.scan(%r{https://example.com/(root|child|grandchild)?/account.css})).to eql [['root'], ['child'], ['grandchild']]
end
end
it "should not include sub-account css when root account is context" do
@context = @domain_root_account
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output.scan(%r{https://example.com/(root|child|grandchild)?/account.css})).to eql [['root']]
end
it "should use include sub-account css, if sub-account is lowest common account context" do
@course = @grandchild_account.courses.create!
@course.offer!
student_in_course(active_all: true)
@context = @user
@current_user = @user
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output.scan(%r{https://example.com/(root|child|grandchild)?/account.css})).to eql [['root'], ['child'], ['grandchild']]
end
it "should work using common_account_chain starting from lowest common account context with enrollments" do
course1 = @child_account.courses.create!
course1.offer!
course2 = @grandchild_account.courses.create!
course2.offer!
student_in_course(active_all: true, course: course1, user: @user)
student_in_course(active_all: true, course: course2, user: @user)
@context = @user
@current_user = @user
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output.scan(%r{https://example.com/(root|child|grandchild)?/account.css})).to eql [['root'], ['child']]
end
it "should fall-back to @domain_root_account's branding if I'm logged in but not enrolled in anything" do
@current_user = user_factory
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output.scan(%r{https://example.com/(root|child|grandchild)?/account.css})).to eql [['root']]
end
it "should load custom css even for high contrast users" do
@current_user = user_factory
user_factory.enable_feature!(:high_contrast)
@context = @grandchild_account
output = helper.include_account_css
expect(output).to have_tag 'link'
expect(output.scan(%r{https://example.com/(root|child|grandchild)?/account.css})).to eql [["root"], ["child"], ["grandchild"]]
end
end
end
describe "include_account_js" do
before :once do
@site_admin = Account.site_admin
@domain_root_account = Account.default
@domain_root_account.settings = @domain_root_account.settings.merge(global_includes: true)
@domain_root_account.save!
end
context "with no custom js" do
it "should be empty" do
allow(helper).to receive(:active_brand_config).and_return(nil)
expect(helper.include_account_js).to be_nil
end
end
context "with custom js" do
it "should include account javascript" do
allow(helper).to receive(:active_brand_config).and_return BrandConfig.create!(js_overrides: 'https://example.com/path/to/overrides.js')
output = helper.include_account_js
expect(output).to have_tag 'script', text: %r{https:\\/\\/example.com\\/path\\/to\\/overrides.js}
end
it "should include site_admin javascript even if there is no active brand" do
allow(helper).to receive(:active_brand_config).and_return nil
Account.site_admin.create_brand_config!({
css_overrides: 'https://example.com/site_admin/account.css',
js_overrides: 'https://example.com/site_admin/account.js'
})
output = helper.include_account_js
expect(output).to have_tag 'script', text: %r{https:\\/\\/example.com\\/site_admin\\/account.js}
end
context "sub-accounts" do
before { set_up_subaccounts }
it "should just include domain root account's when there is no context or @current_user" do
output = helper.include_account_js
expect(output).to have_tag 'script'
expect(output).to eq("<script src=\"https://example.com/root/account.js\" defer=\"defer\"></script>")
end
it "should load custom js even for high contrast users" do
@current_user = user_factory
user_factory.enable_feature!(:high_contrast)
output = helper.include_account_js
expect(output).to eq("<script src=\"https://example.com/root/account.js\" defer=\"defer\"></script>")
end
it "should include granchild, child, and root when viewing the grandchild or any course or group in it" do
course = @grandchild_account.courses.create!
group = course.groups.create!
[@grandchild_account, course, group].each do |context|
@context = context
expect(helper.include_account_js).to eq %{
<script src="https://example.com/root/account.js" defer="defer"></script>
<script src="https://example.com/child/account.js" defer="defer"></script>
<script src="https://example.com/grandchild/account.js" defer="defer"></script>
}.strip
end
end
end
end
end
end
describe "help link" do
before :once do
Setting.set('show_feedback_link', 'true')
end
it "should configure the help link to display the dialog by default" do
expect(helper.show_help_link?).to eq true
expect(helper.help_link_url).to eq '#'
expect(helper.help_link_classes).to eq 'help_dialog_trigger'
end
it "should override default help link with the configured support url" do
support_url = 'http://instructure.com'
Account.default.update_attribute(:settings, { :support_url => support_url })
helper.instance_variable_set(:@domain_root_account, Account.default)
Setting.set('show_feedback_link', 'false')
expect(helper.support_url).to eq support_url
expect(helper.show_help_link?).to eq true
expect(helper.help_link_url).to eq support_url
expect(helper.help_link_icon).to eq 'help'
expect(helper.help_link_classes).to eq 'support_url'
end
it "should return the configured icon" do
icon = 'inbox'
Account.default.update_attribute(:settings, { :help_link_icon => icon })
helper.instance_variable_set(:@domain_root_account, Account.default)
expect(helper.help_link_icon).to eq icon
end
it "should return the configured help link name" do
link_name = 'Links'
Account.default.update_attribute(:settings, { :help_link_name => link_name })
helper.instance_variable_set(:@domain_root_account, Account.default)
expect(helper.help_link_name).to eq link_name
end
end
describe "collection_cache_key" do
it "should generate a cache key, changing when an element cache_key changes" do
collection = [user_factory, user_factory, user_factory]
key1 = collection_cache_key(collection)
key2 = collection_cache_key(collection)
expect(key1).to eq key2
# verify it's not overly long
expect(key1.length).to be <= 40
User.where(:id => collection[1]).update_all(:updated_at => 1.hour.ago)
collection[1].reload
key3 = collection_cache_key(collection)
expect(key1).not_to eq key3
end
end
context "dashboard_url" do
before :once do
@domain_root_account = Account.default
end
it "returns a regular canvas dashboard url" do
expect(dashboard_url).to eq "http://test.host/"
end
context "with a custom dashboard_url on the account" do
before :each do
@domain_root_account.settings[:dashboard_url] = "http://foo.bar"
end
it "returns the custom dashboard_url" do
expect(dashboard_url).to eq "http://foo.bar"
end
context "with login_success=1" do
it "returns a regular canvas dashboard url" do
expect(dashboard_url(:login_success => '1')).to eq "http://test.host/?login_success=1"
end
end
context "with become_user_id=1" do
it "returns a regular canvas dashboard url for masquerading" do
expect(dashboard_url(:become_user_id => '1')).to eq "http://test.host/?become_user_id=1"
end
end
context "with a user logged in" do
before :each do
@current_user = user_factory
end
it "returns the custom dashboard_url with the current user's id" do
expect(dashboard_url).to eq "http://foo.bar?current_user_id=#{@current_user.id}"
end
end
end
end
context "include_custom_meta_tags" do
it "should be nil if @meta_tags is not defined" do
expect(include_custom_meta_tags).to be_nil
end
it "should include tags if present" do
@meta_tags = [{ :name => "hi", :content => "there" }]
result = include_custom_meta_tags
expect(result).to match(/meta/)
expect(result).to match(/name="hi"/)
expect(result).to match(/content="there"/)
end
it "should html_safe-ify them" do
@meta_tags = [{ :name => "hi", :content => "there" }]
expect(include_custom_meta_tags).to be_html_safe
end
end
describe "editor_buttons" do
it "should return hash of tools if in group" do
@course = course_model
@group = @course.groups.create!(:name => "some group")
tool = @course.context_external_tools.new(:name => "bob", :consumer_key => "test", :shared_secret => "secret", :url => "http://example.com")
tool.editor_button = {:url => "http://example.com", :icon_url => "http://example.com", :canvas_icon_class => 'icon-commons'}
tool.save!
@context = @group
expect(editor_buttons).to eq([{:name=>"bob", :id=>tool.id, :url=>"http://example.com", :icon_url=>"http://example.com", :canvas_icon_class => 'icon-commons', :width=>800, :height=>400}])
end
it "should return hash of tools if in course" do
@course = course_model
tool = @course.context_external_tools.new(:name => "bob", :consumer_key => "test", :shared_secret => "secret", :url => "http://example.com")
tool.editor_button = {:url => "http://example.com", :icon_url => "http://example.com", :canvas_icon_class => 'icon-commons'}
tool.save!
allow(controller).to receive(:group_external_tool_path).and_return('http://dummy')
@context = @course
expect(editor_buttons).to eq([{:name=>"bob", :id=>tool.id, :url=>"http://example.com", :icon_url=>"http://example.com", :canvas_icon_class => 'icon-commons', :width=>800, :height=>400}])
end
it "should not include tools from the domain_root_account for users" do
@domain_root_account = Account.default
account_admin_user
tool = @domain_root_account.context_external_tools.new(
:name => "bob",
:consumer_key => "test",
:shared_secret => "secret",
:url => "http://example.com"
)
tool.editor_button = {:url => "http://example.com", :icon_url => "http://example.com"}
tool.save!
@context = @admin
expect(editor_buttons).to be_empty
end
end
describe "UI path checking" do
describe "#active_path?" do
context "when the request path is the course show page" do
let(:request){ double('request', :fullpath => '/courses/2')}
it "returns true for paths that match" do
expect(active_path?('/courses')).to be_truthy
end
it "returns false for paths that don't match" do
expect(active_path?('/grades')).to be_falsey
end
it "returns false for paths that don't start the same" do
expect(active_path?('/accounts/courses')).to be_falsey
end
end
context "when the request path is the account external tools path" do
let(:request){ double('request', :fullpath => '/accounts/2/external_tools/27')}
before :each do
@context = Account.default
allow(controller).to receive(:controller_name).and_return('external_tools')
end
it "it doesn't return true for '/accounts'" do
expect(active_path?('/accounts')).to be_falsey
end
end
context "when the request path is the course external tools path" do
let(:request){ double('request', :fullpath => '/courses/2/external_tools/27')}
before :each do
@context = Account.default.courses.create!
allow(controller).to receive(:controller_name).and_return('external_tools')
end
it "returns true for '/courses'" do
expect(active_path?('/courses')).to be_truthy
end
end
end
end
describe "js_base_url" do
it "returns an immutable string" do
expect(js_base_url).to be_frozen
end
end
describe 'brand_config_for_account' do
it "handles not having @domain_root_account set" do
expect(helper.send(:brand_config_for_account)).to be_nil
end
end
describe "active_brand_config" do
it "returns nil if user prefers high contrast" do
@current_user = user_factory
@current_user.enable_feature!(:high_contrast)
expect(helper.send(:active_brand_config)).to be_nil
end
it "returns 'K12 Theme' by default for a k12 school" do
allow(helper).to receive(:k12?).and_return(true)
allow(BrandConfig).to receive(:k12_config)
expect(helper.send(:active_brand_config)).to eq BrandConfig.k12_config
end
it "returns 'K12 Theme' if a k12 school has chosen 'canvas default' in Theme Editor" do
allow(helper).to receive(:k12?).and_return(true)
allow(BrandConfig).to receive(:k12_config)
# this is what happens if you pick "Canvas Default" from the theme picker
session[:brand_config_md5] = false
expect(helper.send(:active_brand_config)).to eq BrandConfig.k12_config
end
end
describe "include_head_js" do
before do
allow(helper).to receive(:js_bundles).and_return([[:some_bundle], [:some_plugin_bundle, :some_plugin], [:another_bundle, nil]])
end
it "creates the correct javascript tags" do
allow(helper).to receive(:js_env).and_return({
BIGEASY_LOCALE: 'nb_NO',
MOMENT_LOCALE: 'nb',
TIMEZONE: 'America/La_Paz',
CONTEXT_TIMEZONE: 'America/Denver'
})
base_url = helper.use_optimized_js? ? 'dist/webpack-production' : 'dist/webpack-dev'
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/vendor.js').and_return('vendor_url')
allow(Canvas::Cdn::RevManifest).to receive(:revved_url_for).with('timezone/America/La_Paz.js').and_return('La_Paz_url')
allow(Canvas::Cdn::RevManifest).to receive(:revved_url_for).with('timezone/America/Denver.js').and_return('Denver_url')
allow(Canvas::Cdn::RevManifest).to receive(:revved_url_for).with('timezone/nb_NO.js').and_return('nb_NO_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/moment/locale/nb.js').and_return('nb_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/appBootstrap.js').and_return('app_bootstrap_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/common.js').and_return('common_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/some_bundle.js').and_return('some_bundle_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/some_plugin-some_plugin_bundle.js').and_return('plugin_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/another_bundle.js').and_return('another_bundle_url')
expect(helper.include_head_js).to eq %{
<script src="/vendor_url" defer="defer"></script>
<script src="/La_Paz_url" defer="defer"></script>
<script src="/Denver_url" defer="defer"></script>
<script src="/nb_NO_url" defer="defer"></script>
<script src="/nb_url" defer="defer"></script>
<script src="/app_bootstrap_url" defer="defer"></script>
<script src="/common_url" defer="defer"></script>
<script src="/some_bundle_url" defer="defer"></script>
<script src="/plugin_url" defer="defer"></script>
<script src="/another_bundle_url" defer="defer"></script>
}.strip
end
end
describe "include_js_bundles" do
before do
allow(helper).to receive(:js_bundles).and_return([[:some_bundle], [:some_plugin_bundle, :some_plugin], [:another_bundle, nil]])
end
it "creates the correct javascript tags" do
base_url = helper.use_optimized_js? ? 'dist/webpack-production' : 'dist/webpack-dev'
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/some_bundle.js').and_return('some_bundle_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/some_plugin-some_plugin_bundle.js').and_return('plugin_url')
allow(Canvas::Cdn::RevManifest).to receive(:webpack_url_for).with(base_url + '/another_bundle.js').and_return('another_bundle_url')
expect(helper.include_js_bundles).to eq %{
<script src="/some_bundle_url" defer="defer"></script>
<script src="/plugin_url" defer="defer"></script>
<script src="/another_bundle_url" defer="defer"></script>
}.strip
end
end
describe "map_courses_for_menu" do
context "with Dashcard Reordering feature enabled" do
before(:each) do
@account = Account.default
@account.enable_feature! :dashcard_reordering
@domain_root_account = @account
end
it "returns the list of courses sorted by position" do
course1 = @account.courses.create!
course2 = @account.courses.create!
course3 = @account.courses.create!
user = user_model
course1.enroll_student(user)
course2.enroll_student(user)
course3.enroll_student(user)
courses = [course1, course2, course3]
user.dashboard_positions[course1.asset_string] = 3
user.dashboard_positions[course2.asset_string] = 2
user.dashboard_positions[course3.asset_string] = 1
user.save!
@current_user = user
mapped_courses = map_courses_for_menu(courses)
expect(mapped_courses.map {|h| h[:id]}).to eq [course3.id, course2.id, course1.id]
end
end
end
describe "tutorials_enabled?" do
before(:each) do
@domain_root_account = Account.default
end
context "with new_users_tutorial feature flag enabled" do
before(:each) do
@domain_root_account.enable_feature! :new_user_tutorial
@current_user = User.create!
end
it "returns true if the user has the flag enabled" do
@current_user.enable_feature!(:new_user_tutorial_on_off)
expect(tutorials_enabled?).to be true
end
it "returns false if the user has the flag disabled" do
@current_user.disable_feature!(:new_user_tutorial_on_off)
expect(tutorials_enabled?).to be false
end
end
end
describe "planner_enabled?" do
before(:each) do
@domain_root_account = Account.default
end
context "with student_planner feature flag enabled" do
before(:each) do
@domain_root_account.enable_feature! :student_planner
end
it "returns false when a user has no student enrollments" do
course_with_teacher(:active_all => true)
@current_user = @user
expect(planner_enabled?).to be false
end
it "returns true when there is at least one student enrollment" do
course_with_student(:active_all => true)
@current_user = @user
expect(planner_enabled?).to be true
end
end
context "with student_planner feature flag disabled" do
it "returns false" do
expect(planner_enabled?).to be false
end
end
end
end