<commit_before>module CoronavirusFeatureSteps
  def given_i_am_a_coronavirus_editor
    stub_user.permissions << "Coronavirus editor"
    stub_user.name = "Test author"
  end

  def given_i_can_access_unreleased_features
    stub_user.permissions << "Unreleased feature"
  end

  def given_there_is_a_coronavirus_page
    @coronavirus_page = FactoryBot.create(:coronavirus_page)
  end

  def given_there_is_a_published_coronavirus_page
    @coronavirus_page = FactoryBot.create(:coronavirus_page, state: "published")
  end

  def live_coronavirus_content_item
    File.read(Rails.root.join("spec/fixtures/coronavirus_content_item.json"))
  end

  def coronavirus_content_json
    @coronavirus_content_json ||= JSON.parse(live_coronavirus_content_item)
  end

  def coronavirus_content_id
    coronavirus_content_json["content_id"]
  end

  def todays_date
    Time.zone.now.strftime("%-d %B %Y")
  end

  def stub_coronavirus_publishing_api
    stub_live_coronavirus_content_request
    stub_any_publishing_api_put_content
    stub_any_publishing_api_publish
  end

  def stub_live_coronavirus_content_request
    stub_publishing_api_has_item(coronavirus_content_json)
  end

  def then_the_content_is_sent_to_publishing_api
    assert_publishing_api_put_content(
      "774cee22-d896-44c1-a611-e3109cce8eae",
      request_json_includes(
        "title" => "Coronavirus (COVID-19): what you need to do",
      ),
    )
  end

  def then_i_see_an_edit_landing_page_link
    expect(page).to have_link(I18n.t("coronavirus.pages.index.landing_page_edit.sections"))
  end

  def and_i_select_landing_page
    click_link(I18n.t("coronavirus.pages.index.landing_page_edit.something_else"))
  end

  def when_i_visit_the_coronavirus_index_page
    visit "/coronavirus"
  end

  def when_i_visit_a_coronavirus_page
    visit "/coronavirus/landing"
  end

  ### Reordering sections spec ##

  def when_i_visit_the_reorder_page
    visit "/coronavirus/landing/sub_sections/reorder"
  end

  # Editing the header section

  def then_i_can_see_a_header_section
    expect(page).to have_content(I18n.t("coronavirus.pages.show.header_section.title"))
  end

  def when_i_edit_the_header_section
    page.find("a[href=\"/coronavirus/landing/edit-header\"]", text: "Change").click
  end

  def then_i_can_see_the_edit_header_form
    expect(page).to have_text(I18n.t("coronavirus.pages.edit_header.form.body.label"))
    expect(page).to have_text(I18n.t("coronavirus.pages.edit_header.form.header_link.title"))
  end

  def when_i_fill_in_the_edit_header_form_with_valid_data
    fill_in("header_title", with: "Fancy title")
    fill_in("header_body", with: "##Form content")
    fill_in("header_link_pre_wrap_text", with: "Pre wrap text")
    fill_in("header_link_post_wrap_text", with: "Post wrap text")
    fill_in("header_link_url", with: "/link")
    click_on("Save")
  end

  def then_i_see_header_updated_message
    expect(page).to have_text(I18n.t("coronavirus.pages.update_header.success"))
  end

  def set_up_basic_sub_sections
    @coronavirus_page = FactoryBot.create(:coronavirus_page, state: "published")
    FactoryBot.create(:coronavirus_sub_section,
                      page: @coronavirus_page,
                      position: 0,
                      title: "I am first",
                      sub_heading: "sub heading",
                      content: "###title\n[label](/url?priority-taxon=#{@coronavirus_page.content_id})")
    FactoryBot.create(:coronavirus_sub_section,
                      page: @coronavirus_page,
                      position: 1,
                      title: "I am second",
                      sub_heading: nil,
                      content: "###title\n[label](/url?priority-taxon=#{@coronavirus_page.content_id})")
    stub_live_sub_sections_content_request(@coronavirus_page.content_id)
  end

  def coronavirus_content_json_with_sections(content_id)
    path = Rails.root.join("spec/fixtures/coronavirus_page_sections.json")
    JSON.parse(File.read(path).gsub("774cee22-d896-44c1-a611-e3109cce8eae", content_id))
  end

  def stub_live_sub_sections_content_request(content_id)
    content = coronavirus_content_json_with_sections(content_id)
    stub_publishing_api_has_item(content)
  end

  def stub_discard_subsection_changes
    stub_publishing_api_discard_draft(@coronavirus_page.content_id)
  end

  def stub_discard_coronavirus_page_draft
    stub_publishing_api_discard_draft(coronavirus_content_id)
  end

  def stub_discard_coronavirus_page_no_draft
    stub_live_sub_sections_content_request(@coronavirus_page.content_id)
    stub_any_publishing_api_discard_draft
      .to_return(status: 422, body: "You do not have a draft to discard")
  end

  def i_see_subsection_one_in_position_one
    expect(find(".gem-c-reorderable-list:first-child")).to have_content "I am first"<commit_msg>Remove unused stub method<commit_after>module CoronavirusFeatureSteps
  def given_i_am_a_coronavirus_editor
    stub_user.permissions << "Coronavirus editor"
    stub_user.name = "Test author"
  end

  def given_i_can_access_unreleased_features
    stub_user.permissions << "Unreleased feature"
  end

  def given_there_is_a_coronavirus_page
    @coronavirus_page = FactoryBot.create(:coronavirus_page)
  end

  def given_there_is_a_published_coronavirus_page
    @coronavirus_page = FactoryBot.create(:coronavirus_page, state: "published")
  end

  def live_coronavirus_content_item
    File.read(Rails.root.join("spec/fixtures/coronavirus_content_item.json"))
  end

  def coronavirus_content_json
    @coronavirus_content_json ||= JSON.parse(live_coronavirus_content_item)
  end

  def todays_date
    Time.zone.now.strftime("%-d %B %Y")
  end

  def stub_coronavirus_publishing_api
    stub_live_coronavirus_content_request
    stub_any_publishing_api_put_content
    stub_any_publishing_api_publish
  end

  def stub_live_coronavirus_content_request
    stub_publishing_api_has_item(coronavirus_content_json)
  end

  def then_the_content_is_sent_to_publishing_api
    assert_publishing_api_put_content(
      "774cee22-d896-44c1-a611-e3109cce8eae",
      request_json_includes(
        "title" => "Coronavirus (COVID-19): what you need to do",
      ),
    )
  end

  def then_i_see_an_edit_landing_page_link
    expect(page).to have_link(I18n.t("coronavirus.pages.index.landing_page_edit.sections"))
  end

  def and_i_select_landing_page
    click_link(I18n.t("coronavirus.pages.index.landing_page_edit.something_else"))
  end

  def when_i_visit_the_coronavirus_index_page
    visit "/coronavirus"
  end

  def when_i_visit_a_coronavirus_page
    visit "/coronavirus/landing"
  end

  ### Reordering sections spec ##

  def when_i_visit_the_reorder_page
    visit "/coronavirus/landing/sub_sections/reorder"
  end

  # Editing the header section

  def then_i_can_see_a_header_section
    expect(page).to have_content(I18n.t("coronavirus.pages.show.header_section.title"))
  end

  def when_i_edit_the_header_section
    page.find("a[href=\"/coronavirus/landing/edit-header\"]", text: "Change").click
  end

  def then_i_can_see_the_edit_header_form
    expect(page).to have_text(I18n.t("coronavirus.pages.edit_header.form.body.label"))
    expect(page).to have_text(I18n.t("coronavirus.pages.edit_header.form.header_link.title"))
  end

  def when_i_fill_in_the_edit_header_form_with_valid_data
    fill_in("header_title", with: "Fancy title")
    fill_in("header_body", with: "##Form content")
    fill_in("header_link_pre_wrap_text", with: "Pre wrap text")
    fill_in("header_link_post_wrap_text", with: "Post wrap text")
    fill_in("header_link_url", with: "/link")
    click_on("Save")
  end

  def then_i_see_header_updated_message
    expect(page).to have_text(I18n.t("coronavirus.pages.update_header.success"))
  end

  def set_up_basic_sub_sections
    @coronavirus_page = FactoryBot.create(:coronavirus_page, state: "published")
    FactoryBot.create(:coronavirus_sub_section,
                      page: @coronavirus_page,
                      position: 0,
                      title: "I am first",
                      sub_heading: "sub heading",
                      content: "###title\n[label](/url?priority-taxon=#{@coronavirus_page.content_id})")
    FactoryBot.create(:coronavirus_sub_section,
                      page: @coronavirus_page,
                      position: 1,
                      title: "I am second",
                      sub_heading: nil,
                      content: "###title\n[label](/url?priority-taxon=#{@coronavirus_page.content_id})")
    stub_live_sub_sections_content_request(@coronavirus_page.content_id)
  end

  def coronavirus_content_json_with_sections(content_id)
    path = Rails.root.join("spec/fixtures/coronavirus_page_sections.json")
    JSON.parse(File.read(path).gsub("774cee22-d896-44c1-a611-e3109cce8eae", content_id))
  end

  def stub_live_sub_sections_content_request(content_id)
    content = coronavirus_content_json_with_sections(content_id)
    stub_publishing_api_has_item(content)
  end

  def stub_discard_subsection_changes
    stub_publishing_api_discard_draft(@coronavirus_page.content_id)
  end

  def stub_discard_coronavirus_page_no_draft
    stub_live_sub_sections_content_request(@coronavirus_page.content_id)
    stub_any_publishing_api_discard_draft
      .to_return(status: 422, body: "You do not have a draft to discard")
  end

  def i_see_subsection_one_in_position_one
    expect(find(".gem-c-reorderable-list:first-child")).to have_content "I am first"
{'fraction_non_alphanumeric': 0.08723135271807839, 'fraction_numerical': 0.012010113780025285, 'mean_word_length': 4.215934065934066, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3004952', 'n_tokens_mistral': 3415, 'n_tokens_neox': 3283, 'n_words': 454}