<commit_before>require 'rails_helper'
require 'capybara/rails'

RSpec.describe "unpublishing guides", type: :feature do
  context "with a published guide" do
    before do
      allow_any_instance_of(RedirectPublisher).to receive(:process)
      allow_any_instance_of(GuideSearchIndexer).to receive(:delete)
    end

    it "creates a Redirect, sets the guide state to unpublished and shows who unpublished in the history" do
      bob = create(:user, name: "Bob")
      GDS::SSO.test_user = bob

      guide = create(:published_guide, title: "Scrum")
      topic = create(:topic, path: "/service-manual/agile-delivery")

      expect_any_instance_of(RedirectPublisher).to receive(:process).with(
        content_id: anything,
        old_path:   guide.slug,
        new_path:   topic.path,
      )

      visit edit_guide_path(guide)
      click_first_link "Unpublish"
      select topic.path, from: "Redirect to"
      click_button "Unpublish"

      # We are storing where the user redirected to in the `redirects`
      # table but we aren't displaying in the browser yet. Therefore
      # we are testing it here.
      expect(
        Redirect.find_by(old_path: guide.slug, new_path: topic.path)
        ).to be_present

      # Assert the guide is unpublished
      expect(current_path).to eq(root_path)
      within_guide_index_row("Scrum") do
        expect(page).to have_content("Unpublished")
      end

      # Assert the current user did the unpublishing on the history tab
      visit(guide_editions_path(guide))
      within_guide_history_edition(1) do
        expect(page).to have_content("Unpublished by Bob")
      end
    end

    context "before we stored who created an edition" do
      it "creates a Redirect record and marks as unpublished" do
        guide = create(:published_guide)
        latest_edition = guide.latest_edition
        latest_edition.created_by_id = nil
        latest_edition.save(validate: false)
        topic = create(:topic, path: "/service-manual/agile-delivery")

        expect_any_instance_of(RedirectPublisher).to receive(:process).with(
          content_id: anything,
          old_path:   guide.slug,
          new_path:   topic.path,
        )

        visit edit_guide_path(guide)
        click_first_link "Unpublish"
        select topic.path, from: "Redirect to"
        click_button "Unpublish"

        expect(Redirect.count).to eq 1
        expect(Redirect.first.old_path).to eq guide.slug
        expect(Redirect.first.new_path).to eq topic.path
        expect(guide.reload.latest_edition).to be_unpublished
      end
    end

    it "disables all form submits in the guide editor" do
      guide = create(:unpublished_guide)

      visit edit_guide_path(guide)

      expect(page).to_not have_button "Save"
      expect(page).to_not have_button "Send for review"
      expect(page).to_not have_button "Discard new guide"
      expect(page).to_not have_button "Discard draft"
      expect(page).to_not have_link "Unpublish"
    end

    it "removes the guide from the search index" do
      guide = create(:published_guide)
      new_guide = create(:published_guide)

      allow_any_instance_of(RedirectPublisher).to receive(:process)

      indexer = double(:indexer)<commit_msg>Improve readability of an unpublishing test<commit_after>require 'rails_helper'
require 'capybara/rails'

RSpec.describe "unpublishing guides", type: :feature do
  context "with a published guide" do
    before do
      allow_any_instance_of(RedirectPublisher).to receive(:process)
      allow_any_instance_of(GuideSearchIndexer).to receive(:delete)
    end

    it "creates a Redirect, sets the guide state to unpublished and shows who unpublished in the history" do
      bob = create(:user, name: "Bob")
      GDS::SSO.test_user = bob

      guide = create(:published_guide, title: "Scrum")
      topic = create(:topic, path: "/service-manual/agile-delivery")

      expect_any_instance_of(RedirectPublisher).to receive(:process).with(
        content_id: anything,
        old_path:   guide.slug,
        new_path:   topic.path,
      )

      visit edit_guide_path(guide)
      click_first_link "Unpublish"
      select topic.path, from: "Redirect to"
      click_button "Unpublish"

      # We are storing where the user redirected to in the `redirects`
      # table but we aren't displaying in the browser yet. Therefore
      # we are testing it here.
      expect(
        Redirect.find_by(old_path: guide.slug, new_path: topic.path)
        ).to be_present

      # Assert the guide is unpublished
      expect(current_path).to eq(root_path)
      within_guide_index_row("Scrum") do
        expect(page).to have_content("Unpublished")
      end

      # Assert the current user did the unpublishing on the history tab
      visit(guide_editions_path(guide))
      within_guide_history_edition(1) do
        expect(page).to have_content("Unpublished by Bob")
      end
    end

    context "before we stored who created an edition" do
      it "does not error and sets the guide state to Unpublished" do
        guide = create(:published_guide, title: "Scrum")
        topic = create(:topic, path: "/service-manual/agile-delivery")

        # Fake the situation we have in production where the
        # `editions.created_by_id` field is NULL
        latest_edition = guide.latest_edition
        latest_edition.created_by_id = nil
        latest_edition.save(validate: false)

        expect_any_instance_of(RedirectPublisher).to receive(:process)

        visit edit_guide_path(guide)
        click_first_link "Unpublish"
        select topic.path, from: "Redirect to"
        click_button "Unpublish"

        expect(current_path).to eq(root_path)
        within_guide_index_row("Scrum") do
          expect(page).to have_content("Unpublished")
        end
      end
    end

    it "disables all form submits in the guide editor" do
      guide = create(:unpublished_guide)

      visit edit_guide_path(guide)

      expect(page).to_not have_button "Save"
      expect(page).to_not have_button "Send for review"
      expect(page).to_not have_button "Discard new guide"
      expect(page).to_not have_button "Discard draft"
      expect(page).to_not have_link "Unpublish"
    end

    it "removes the guide from the search index" do
      guide = create(:published_guide)
      new_guide = create(:published_guide)

      allow_any_instance_of(RedirectPublisher).to receive(:process)

      indexer = double(:indexer)
{'fraction_non_alphanumeric': 0.08259996912150687, 'fraction_numerical': 0.0004631773969430292, 'mean_word_length': 3.01363073110285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5606242', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1858, 'n_words': 519}