<commit_before>require 'rails_helper'

def set_up_suite
  include Devise::TestHelpers, type: :feature
  Capybara.current_driver = :selenium
  page.current_window.resize_to(1920, 1080)
  stub_oauth_edit
end

def fill_out_course_creator_form
  fill_in 'Course title:', with: 'My course'
  fill_in 'Course term:', with: 'Spring 2016'
  fill_in 'Course school:', with: 'University of Oklahoma'
  find('input[placeholder="Start date (YYYY-MM-DD)"]').set(Date.new(2015, 1, 4))
  find('input[placeholder="End date (YYYY-MM-DD)"]').set(Date.new(2015, 2, 1))
  find('div.wizard__panel').click # click to escape the calendar popup
  click_button 'Create my Course!'
end

def interact_with_clone_form
  fill_in 'Course term:', with: 'Spring 2016'
  fill_in 'Course description:', with: 'A new course'

  find('input[placeholder="Start date (YYYY-MM-DD)"]').set(course.start)
  find('input[placeholder="End date (YYYY-MM-DD)"]').set(course.end)

  within('.wizard__form') { click_button 'Save New Course' }

  within('.sidebar') { expect(page).to have_content(term) }
  within('.primary') { expect(page).to have_content(desc) }

  click_button 'Save This Course'
end

def go_through_course_dates_and_timeline_dates
  find('attr[title="Wednesday"]', match: :first).click
  within('.wizard__panel.active') do
    expect(page).to have_css('button.dark[disabled=""]')
  end
  find('.wizard__form.course-dates input[type=checkbox]', match: :first).set(true)
  within('.wizard__panel.active') do
    expect(page).not_to have_css('button.dark[disabled=disabled]')
  end

  click_button 'Next'
  sleep 1
end

def go_through_researchwrite_wizard
  go_through_course_dates_and_timeline_dates

  # Choose researchwrite option
  find('.wizard__option', match: :first).find('button', match: :first).click
  click_button 'Next'
  sleep 1

  # Click through the offered choices
  find('.wizard__option', match: :first).find('button', match: :first).click # Training not graded
  click_button 'Next'
  sleep 1

  click_button 'Next' # Default getting started options
  sleep 1

  # Instructor prepares list
  find('.wizard__option', match: :first).find('button', match: :first).click
  click_button 'Next'
  sleep 1

  find('.wizard__option', match: :first).find('button', match: :first).click # Yes, medical articles
  click_button 'Next'
  sleep 1

  click_button 'Next' # Default 2 peer reviews
  sleep 1

  click_button 'Next' # No supplementary assignments
  sleep 1

  click_button 'Next' # No DYK/GA
  sleep 1

  click_button 'Submit'
  sleep 1
end

describe 'New course creation and editing', type: :feature do
  before do
    set_up_suite
  end

  before :each do
    user = create(:user,
                  id: 1,
                  permissions: User::Permissions::INSTRUCTOR)
    create(:training_modules_users, user_id: user.id,
                                    training_module_id: 3,
                                    completed_at: Time.now)
    login_as(user, scope: :user)

    visit root_path
  end

  describe 'course workflow', js: true do
    let(:expected_course_blocks) { 27 }
    let(:module_name) { 'Wikipedia Essentials' }

    it 'should allow the user to create a course' do
      click_link 'Create Course'

      expect(page).to have_content 'Create a New Course'
      find('#course_title').set('My awesome new course - Foo 101')

      # If we click before filling out all require fields, only the invalid
      # fields get restyled to indicate the problem.
      click_button 'Create my Course!'
      expect(find('#course_title')['class']).not_to include('invalid title')
      expect(find('#course_school')['class']).to include('invalid school')
      expect(find('#course_term')['class']).to include('invalid term')

      # Now we fill out all the fields and continue.
      find('#course_school').set('University of Wikipedia, East Campus')
      find('#course_term').set('Fall 2015')
      find('#course_subject').set('Advanced Studies')
      find('#course_expected_students').set('500')
      find('textarea').set('In this course, we study things.')

      sleep 1

      start_date = '2015-01-01'
      end_date = '2015-12-15'
      find('input[placeholder="Start date (YYYY-MM-DD)"]').set(start_date)
      find('div.DayPicker-Day--selected', text: '1').click
      find('input[placeholder="End date (YYYY-MM-DD)"]').set('2015-12-01')
      find('div.DayPicker-Day', text: '15').click

      sleep 1

      # This click should create the course and start the wizard
      click_button 'Create my Course!'

      # Go through the wizard, checking necessary options.

      # This is the course dates screen
      sleep 3
      # validate either blackout date chosen
      # or "no blackout dates" checkbox checked
      expect(page).to have_css('button.dark[disabled=""]')
      start_input = find('input.start', match: :first).value
      sleep 1
      expect(start_input).to eq(start_date)
      end_input = find('input.end', match: :first).value
      expect(end_input).to eq(end_date)

      # capybara doesn't like trying to click the calendar
      # to set a blackout date
      go_through_course_dates_and_timeline_dates

      # This is the assignment type chooser
      # pick and choose
      page.all('.wizard__option')[1].first('button').click
      sleep 1
      click_button 'Next'
      sleep 1
      # pick 2 types of assignments
      page.all('div.wizard__option__checkbox')[1].click
      page.all('div.wizard__option__checkbox')[3].click
      sleep 1
      click_button 'Next'

      # on the summary
      sleep 1
      # go back to the pick and choose and choose different assignments
      page.all('button.wizard__option.summary')[2].click
      sleep 1
      page.all('div.wizard__option__checkbox')[3].click
      page.all('div.wizard__option__checkbox')[2].click
      page.all('div.wizard__option__checkbox')[4].click
      sleep 1
      click_button 'Summary'
      sleep 1
      click_button 'Submit'

      # Now we're back at the timeline, having completed the wizard.
      sleep 1
      expect(page).to have_content 'Week 1'
      expect(page).to have_content 'Week 2'

      # Edit course dates and save
      click_link 'Edit Course Dates'
      find('attr[title="Thursday"]', match: :first).click
      click_link 'Done'
      sleep 1
      expect(Course.last.weekdays).to eq('0001100')

      within('.week-1 .week__week-add-delete') do
        accept_confirm do
          find('.week__delete-week').click
        end
      end

      # There should now be 4 weeks
      expect(page).not_to have_content 'Week 5'

      # Click edit, mark a gradeable and save it.
      find('.week-1').hover
      sleep 0.5
      within('.week-1') do
        find('.block__edit-block').click
        find('p.graded input[type=checkbox]').set(true)
        sleep 1
        click_button 'Save'
      end
      sleep 1

      # Edit the gradeable.
      within('.grading__grading-container') do
        click_button 'Edit'
        sleep 1
        all('input').last.set('50')
        sleep 1
        click_button 'Save'
        sleep 1
        expect(page).to have_content 'Value: 50%'
      end

      # Navigate back to overview, check relevant data, then delete course
      visit "/courses/#{Course.first.slug}"

      within('.sidebar') do
        expect(page).to have_content I18n.t('courses.instructor.other')
      end

      accept_prompt(with: 'My awesome new course - Foo 101') do
        find('button.danger', match: :first).click
      end

      expect(page).to have_content 'Looks like you don\'t have any courses'
    end

    it 'should not allow a second course with the same slug' do
      create(:course,
             id: 10001,
             title: 'Course',
             school: 'University',
             term: 'Term',
             slug: 'University/Course_(Term)',
             submitted: 0,
             listed: true,
             passcode: '<PASSWORD>',
             start: '2015-08-24'.to_date,
             end: '2015-12-15'.to_date,
             timeline_start: '2015-08-31'.to_date,
             timeline_end: '2015-12-15'.to_date)

      click_link 'Create Course'
      expect(page).to have_content 'Create a New Course'
      find('#course_title').set('Course')
      find('#course_school').set('University')
      find('#course_term').set('Term')
      find('#course_subject').set('Advanced Studies')

      start_date = '2015-01-01'
      end_date = '2015-12-15'
      find('input[placeholder="Start date (YYYY-MM-DD)"]').set(start_date)
      find('input[placeholder="End date (YYYY-MM-DD)"]').set(end_date)
      find('div.wizard__panel').click # click to escape the calendar popup

      # This click should not successfully create a course.
      click_button 'Create my Course!'
      expect(page).to have_content 'This course already exists'
      expect(Course.all.count).to eq(1)
    end

    it 'should create a full-length research-write assignment' do
      create(:course,
             id: 10001,
             title: 'Course',
             school: 'University',
             term: 'Term',
             slug: 'University/Course_(Term)',
             submitted: 0,
             listed: true,
             passcode: '<PASSWORD>',
             start: '2015-08-24'.to_date,
             end: '2015-12-15'.to_date,
             timeline_start: '2015-08-31'.to_date,
             timeline_end: '2015-12-15'.to_date)
      create(:courses_user,
             user_id: 1,
             course_id: 10001,
             role: 1)

      # Visit timline and open wizard
      visit "/courses/#{Course.first.slug}/timeline/wizard"
      sleep 1

      go_through_researchwrite_wizard

      sleep 1

      expect(page).to have_content 'Week 14'

      # Now submit the course
      first('a.button').click
      prompt = page.driver.browser.switch_to.alert
      prompt.accept
      expect(page).to have_content 'Your course has been submitted.'

      Course.last.weeks.each_with_index do |week, i|
        expect(week.order).to eq(i + 1)
      end
      expect(Course.first.blocks.count).to eq(expected_course_blocks)
    end
<commit_msg>More updates for broken wizard spec<commit_after>require 'rails_helper'

def set_up_suite
  include Devise::TestHelpers, type: :feature
  Capybara.current_driver = :selenium
  page.current_window.resize_to(1920, 1080)
  stub_oauth_edit
end

def fill_out_course_creator_form
  fill_in 'Course title:', with: 'My course'
  fill_in 'Course term:', with: 'Spring 2016'
  fill_in 'Course school:', with: 'University of Oklahoma'
  find('input[placeholder="Start date (YYYY-MM-DD)"]').set(Date.new(2015, 1, 4))
  find('input[placeholder="End date (YYYY-MM-DD)"]').set(Date.new(2015, 2, 1))
  find('div.wizard__panel').click # click to escape the calendar popup
  click_button 'Create my Course!'
end

def interact_with_clone_form
  fill_in 'Course term:', with: 'Spring 2016'
  fill_in 'Course description:', with: 'A new course'

  find('input[placeholder="Start date (YYYY-MM-DD)"]').set(course.start)
  find('input[placeholder="End date (YYYY-MM-DD)"]').set(course.end)

  within('.wizard__form') { click_button 'Save New Course' }

  within('.sidebar') { expect(page).to have_content(term) }
  within('.primary') { expect(page).to have_content(desc) }

  click_button 'Save This Course'
end

def go_through_course_dates_and_timeline_dates
  find('attr[title="Wednesday"]', match: :first).click
  within('.wizard__panel.active') do
    expect(page).to have_css('button.dark[disabled=""]')
  end
  find('.wizard__form.course-dates input[type=checkbox]', match: :first).set(true)
  within('.wizard__panel.active') do
    expect(page).not_to have_css('button.dark[disabled=disabled]')
  end

  click_button 'Next'
  sleep 1
end

def go_through_researchwrite_wizard
  go_through_course_dates_and_timeline_dates

  # Choose researchwrite option
  find('.wizard__option', match: :first).find('button', match: :first).click
  click_button 'Next'
  sleep 1

  # Click through the offered choices
  find('.wizard__option', match: :first).find('button', match: :first).click # Training not graded
  click_button 'Next'
  sleep 1

  click_button 'Next' # Default getting started options
  sleep 1

  # Instructor prepares list
  find('.wizard__option', match: :first).find('button', match: :first).click
  click_button 'Next'
  sleep 1

  find('.wizard__option', match: :first).find('button', match: :first).click # Yes, medical articles
  click_button 'Next'
  sleep 1

  click_button 'Next' # Default 2 peer reviews
  sleep 1

  click_button 'Next' # No supplementary assignments
  sleep 1

  click_button 'Next' # No DYK/GA
  sleep 1

  click_button 'Submit'
  sleep 1
end

describe 'New course creation and editing', type: :feature do
  before do
    set_up_suite
  end

  before :each do
    user = create(:user,
                  id: 1,
                  permissions: User::Permissions::INSTRUCTOR)
    create(:training_modules_users, user_id: user.id,
                                    training_module_id: 3,
                                    completed_at: Time.now)
    login_as(user, scope: :user)

    visit root_path
  end

  describe 'course workflow', js: true do
    let(:expected_course_blocks) { 21 }
    let(:module_name) { 'Wikipedia Essentials' }

    it 'should allow the user to create a course' do
      click_link 'Create Course'

      expect(page).to have_content 'Create a New Course'
      find('#course_title').set('My awesome new course - Foo 101')

      # If we click before filling out all require fields, only the invalid
      # fields get restyled to indicate the problem.
      click_button 'Create my Course!'
      expect(find('#course_title')['class']).not_to include('invalid title')
      expect(find('#course_school')['class']).to include('invalid school')
      expect(find('#course_term')['class']).to include('invalid term')

      # Now we fill out all the fields and continue.
      find('#course_school').set('University of Wikipedia, East Campus')
      find('#course_term').set('Fall 2015')
      find('#course_subject').set('Advanced Studies')
      find('#course_expected_students').set('500')
      find('textarea').set('In this course, we study things.')

      sleep 1

      start_date = '2015-01-01'
      end_date = '2015-12-15'
      find('input[placeholder="Start date (YYYY-MM-DD)"]').set(start_date)
      find('div.DayPicker-Day--selected', text: '1').click
      find('input[placeholder="End date (YYYY-MM-DD)"]').set('2015-12-01')
      find('div.DayPicker-Day', text: '15').click

      sleep 1

      # This click should create the course and start the wizard
      click_button 'Create my Course!'

      # Go through the wizard, checking necessary options.

      # This is the course dates screen
      sleep 3
      # validate either blackout date chosen
      # or "no blackout dates" checkbox checked
      expect(page).to have_css('button.dark[disabled=""]')
      start_input = find('input.start', match: :first).value
      sleep 1
      expect(start_input).to eq(start_date)
      end_input = find('input.end', match: :first).value
      expect(end_input).to eq(end_date)

      # capybara doesn't like trying to click the calendar
      # to set a blackout date
      go_through_course_dates_and_timeline_dates

      # This is the assignment type chooser
      # pick and choose
      page.all('.wizard__option')[1].first('button').click
      sleep 1
      click_button 'Next'
      sleep 1
      # pick 2 types of assignments
      page.all('div.wizard__option__checkbox')[1].click
      page.all('div.wizard__option__checkbox')[3].click
      sleep 1
      click_button 'Next'

      # on the summary
      sleep 1
      # go back to the pick and choose and choose different assignments
      page.all('button.wizard__option.summary')[2].click
      sleep 1
      page.all('div.wizard__option__checkbox')[3].click
      page.all('div.wizard__option__checkbox')[2].click
      page.all('div.wizard__option__checkbox')[4].click
      sleep 1
      click_button 'Summary'
      sleep 1
      click_button 'Submit'

      # Now we're back at the timeline, having completed the wizard.
      sleep 1
      expect(page).to have_content 'Week 1'
      expect(page).to have_content 'Week 2'

      # Edit course dates and save
      click_link 'Edit Course Dates'
      find('attr[title="Thursday"]', match: :first).click
      click_link 'Done'
      sleep 1
      expect(Course.last.weekdays).to eq('0001100')

      within('.week-1 .week__week-add-delete') do
        accept_confirm do
          find('.week__delete-week').click
        end
      end

      # There should now be 4 weeks
      expect(page).not_to have_content 'Week 5'

      # Click edit, mark a gradeable and save it.
      find('.week-1').hover
      sleep 0.5
      within('.week-1') do
        find('.block__edit-block').click
        find('p.graded input[type=checkbox]').set(true)
        sleep 1
        click_button 'Save'
      end
      sleep 1

      # Edit the gradeable.
      within('.grading__grading-container') do
        click_button 'Edit'
        sleep 1
        all('input').last.set('50')
        sleep 1
        click_button 'Save'
        sleep 1
        expect(page).to have_content 'Value: 50%'
      end

      # Navigate back to overview, check relevant data, then delete course
      visit "/courses/#{Course.first.slug}"

      within('.sidebar') do
        expect(page).to have_content I18n.t('courses.instructor.other')
      end

      accept_prompt(with: 'My awesome new course - Foo 101') do
        find('button.danger', match: :first).click
      end

      expect(page).to have_content 'Looks like you don\'t have any courses'
    end

    it 'should not allow a second course with the same slug' do
      create(:course,
             id: 10001,
             title: 'Course',
             school: 'University',
             term: 'Term',
             slug: 'University/Course_(Term)',
             submitted: 0,
             listed: true,
             passcode: '<PASSWORD>',
             start: '2015-08-24'.to_date,
             end: '2015-12-15'.to_date,
             timeline_start: '2015-08-31'.to_date,
             timeline_end: '2015-12-15'.to_date)

      click_link 'Create Course'
      expect(page).to have_content 'Create a New Course'
      find('#course_title').set('Course')
      find('#course_school').set('University')
      find('#course_term').set('Term')
      find('#course_subject').set('Advanced Studies')

      start_date = '2015-01-01'
      end_date = '2015-12-15'
      find('input[placeholder="Start date (YYYY-MM-DD)"]').set(start_date)
      find('input[placeholder="End date (YYYY-MM-DD)"]').set(end_date)
      find('div.wizard__panel').click # click to escape the calendar popup

      # This click should not successfully create a course.
      click_button 'Create my Course!'
      expect(page).to have_content 'This course already exists'
      expect(Course.all.count).to eq(1)
    end

    it 'should create a full-length research-write assignment' do
      create(:course,
             id: 10001,
             title: 'Course',
             school: 'University',
             term: 'Term',
             slug: 'University/Course_(Term)',
             submitted: 0,
             listed: true,
             passcode: '<PASSWORD>',
             start: '2015-08-24'.to_date,
             end: '2015-12-15'.to_date,
             timeline_start: '2015-08-31'.to_date,
             timeline_end: '2015-12-15'.to_date)
      create(:courses_user,
             user_id: 1,
             course_id: 10001,
             role: 1)

      # Visit timline and open wizard
      visit "/courses/#{Course.first.slug}/timeline/wizard"
      sleep 1

      go_through_researchwrite_wizard

      sleep 1

      expect(page).to have_content 'Week 12'

      # Now submit the course
      first('a.button').click
      prompt = page.driver.browser.switch_to.alert
      prompt.accept
      expect(page).to have_content 'Your course has been submitted.'

      Course.last.weeks.each_with_index do |week, i|
        expect(week.order).to eq(i + 1)
      end
      expect(Course.first.blocks.count).to eq(expected_course_blocks)
    end

{'fraction_non_alphanumeric': 0.12000790826413603, 'fraction_numerical': 0.02332937920126532, 'mean_word_length': 2.7875327592661923, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5200853', 'n_tokens_mistral': 6567, 'n_tokens_neox': 6017, 'n_words': 1593}