<commit_before>require_relative '../integration_test_helper'
require 'gds_api/test_helpers/need_api'

class ArtefactsEditTest < ActionDispatch::IntegrationTest
  include GdsApi::TestHelpers::NeedApi

  setup do
    create_test_user
  end

  def stub_basic_need_api_response(need_id = "100123")
    need_api_has_need(
      "id" => need_id,
      "role" => "user",
      "goal" => "do something",
      "benefit" => "reason"
    )
  end

  should "link to the live site from the edit form" do
    artefact = FactoryGirl.create(:artefact, :name => "Alpha", :slug => 'alpha')

    visit "/artefacts"
    click_on "Alpha"

    assert page.has_link?("/alpha", :href => "http://www.#{ENV["GOVUK_APP_DOMAIN"]}/alpha")
  end

  # skip all these tests, odi does not make use of needs
  context "displaying need information" do
    context "when a Maslow need ID present" do
      setup do
        @artefact = FactoryGirl.create(:artefact, :need_id => "100123")
      end

      should "show basic information about a need from the API" do
        skip("we do not care about needs. Form is disabled.")
        need_api_has_need(
          "id" => "100123",<commit_msg>First pass at a new integration test for the edit form<commit_after>require_relative '../integration_test_helper'
require 'gds_api/test_helpers/need_api'

class ArtefactsEditTest < ActionDispatch::IntegrationTest
  include GdsApi::TestHelpers::NeedApi

  setup do
    create_test_user
    stub_all_router_api_requests
    stub_all_rummager_requests
  end

  def stub_basic_need_api_response(need_id = "100123")
    need_api_has_need(
      "id" => need_id,
      "role" => "user",
      "goal" => "do something",
      "benefit" => "reason"
    )
  end

  context "editing a publisher artefact" do
    setup do
      FactoryGirl.create(:tag, tag_type: "section", tag_id: "business", parent_id: nil, title: "Business")
      FactoryGirl.create(:tag, tag_type: "section", tag_id: "business/employing-people", parent_id: "business", title: "Employing people")
      FactoryGirl.create(:tag, tag_type: "legacy_source", tag_id: "businesslink", parent_id: nil, title: "Business Link")

      @artefact = FactoryGirl.create(:artefact,
                                     name: "VAT Rates", slug: "vat-rates", kind: "answer", state: "live",
                                     owning_app: "publisher", language: "en", business_proposition: true,
                                     section_ids: ["business/employing-people"], legacy_source_ids: ["businesslink"])
    end

    should "display the artefact form" do
      visit "/artefacts/#{@artefact.id}/edit"

      within "header.artefact-header" do
        assert page.has_selector? "h1", text: "VAT Rates"
        assert page.has_link? "/vat-rates", href: "http://www.dev.gov.uk/vat-rates"
        assert page.has_selector? ".state", text: "live"
      end

      within ".artefact-actions" do
        assert page.has_selector? "li.active", text: "Edit"
        assert page.has_selector? "li:not(.active)", text: "History"
        assert page.has_selector? "li:not(.active)", text: "Archive"
      end

      within ".owning-app" do
        assert page.has_content? "This content is managed in Publisher"
        assert page.has_link? "Edit in Publisher"
      end

      within ".form-actions" do
        assert page.has_button? "Save and continue editing"
        assert page.has_button? "Save and go to item"
      end
    end
  end

  # skip all these tests, odi does not make use of needs
  context "displaying need information" do
    context "when a Maslow need ID present" do
      setup do
        @artefact = FactoryGirl.create(:artefact, :need_id => "100123")
      end

      should "show basic information about a need from the API" do
        skip("we do not care about needs. Form is disabled.")
        need_api_has_need(
          "id" => "100123",
{'fraction_non_alphanumeric': 0.1131198347107438, 'fraction_numerical': 0.009555785123966942, 'mean_word_length': 2.931979695431472, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '354584', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1189, 'n_words': 341}