<commit_before>require 'rails_helper'

feature 'Legislation' do

  let!(:administrator) { create(:administrator).user }

  shared_examples "not published permissions" do |path|

    let(:not_published_process) { create(:legislation_process, :not_published, title: "Process not published") }
    let!(:not_permission_message) { "You do not have permission to carry out the action" }

    it "is forbidden for a normal user" do
      visit send(path, not_published_process)

      expect(page).to have_content not_permission_message
      expect(page).not_to have_content("Process not published")
    end

    it "is available for an administrator user" do
      login_as(administrator)
      visit send(path, not_published_process)

      expect(page).to have_content("Process not published")
    end
  end

  context 'processes home page' do

    scenario 'No processes to be listed' do
      visit legislation_processes_path
      expect(page).to have_text "There aren't open processes"

      visit legislation_processes_path(filter: 'next')
      expect(page).to have_text "There aren't planned processes"

      visit legislation_processes_path(filter: 'past')
      expect(page).to have_text "There aren't past processes"
    end

    scenario 'Processes can be listed' do
      processes = create_list(:legislation_process, 3)

      visit legislation_processes_path

      processes.each do |process|
        expect(page).to have_link(process.title)
      end
    end

    scenario 'Key dates are displayed on current locale' do
      process = create(:legislation_process, proposals_phase_start_date: Date.new(2018, 01, 01),
                                             proposals_phase_end_date: Date.new(2018, 12, 01))

      visit legislation_process_path(process)

      expect(page).to have_content("Proposals")
      expect(page).to have_content("01 Jan 2018 - 01 Dec 2018")

      visit legislation_process_path(process, locale: "es")

      expect(page).to have_content("Propuestas")
      expect(page).to have_content("01 ene 2018 - 01 dic 2018")
    end

    scenario 'Filtering processes' do
      create(:legislation_process, title: "Process open")
      create(:legislation_process, :next, title: "Process next")
      create(:legislation_process, :past, title: "Process past")
      create(:legislation_process, :in_draft_phase, title: "Process in draft phase")

      visit legislation_processes_path
      expect(page).to have_content('Process open')
      expect(page).not_to have_content('Process next')
      expect(page).not_to have_content('Process past')
      expect(page).not_to have_content('Process in draft phase')

      visit legislation_processes_path(filter: 'next')
      expect(page).not_to have_content('Process open')
      expect(page).to have_content('Process next')
      expect(page).not_to have_content('Process past')

      visit legislation_processes_path(filter: 'past')
      expect(page).not_to have_content('Process open')
      expect(page).not_to have_content('Process next')
      expect(page).to have_content('Process past')
    end

    context "not published processes" do
      before do
        create(:legislation_process, title: "published")
        create(:legislation_process, :not_published, title: "not published")
        [:next, :past].each do |trait|
          create(:legislation_process, trait, title: "#{trait} published")
          create(:legislation_process, :not_published, trait, title: "#{trait} not published")
        end
      end

      it "aren't listed" do
        visit legislation_processes_path
        expect(page).not_to have_content('not published')
        expect(page).to have_content('published')

        login_as(administrator)
        visit legislation_processes_path
        expect(page).not_to have_content('not published')
        expect(page).to have_content('published')
      end

      it "aren't listed with next filter" do
        visit legislation_processes_path(filter: 'next')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('next published')

        login_as(administrator)
        visit legislation_processes_path(filter: 'next')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('next published')
      end

      it "aren't listed with past filter" do
        visit legislation_processes_path(filter: 'past')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('past published')

        login_as(administrator)
        visit legislation_processes_path(filter: 'past')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('past published')
      end
    end
  end

  context 'process page' do
    context "show" do
      include_examples "not published permissions", :legislation_process_path

      scenario '#show view has document present' do
        process = create(:legislation_process)
        document = create(:document, documentable: process)
        visit legislation_process_path(process)

        expect(page).to have_content(document.title)
      end

      scenario 'show additional info button' do
        process = create(:legislation_process, additional_info: "Text for additional info of the process")

        visit legislation_process_path(process)

        expect(page).to have_content("Additional information")
        expect(page).to have_content("Text for additional info of the process")
      end

      scenario 'do not show additional info button if it is empty' do
        process = create(:legislation_process)

        visit legislation_process_path(process)

        expect(page).not_to have_content("Additional information")
      end

      scenario "Shows another translation when the default locale isn't available" do
        process = create(:legislation_process, title_fr: "Français")
        process.translations.where(locale: :en).first.destroy
<commit_msg>Adds specs for show view has document present on all phases<commit_after>require 'rails_helper'

feature 'Legislation' do

  let!(:administrator) { create(:administrator).user }

  shared_examples "not published permissions" do |path|

    let(:not_published_process) { create(:legislation_process, :not_published, title: "Process not published") }
    let!(:not_permission_message) { "You do not have permission to carry out the action" }

    it "is forbidden for a normal user" do
      visit send(path, not_published_process)

      expect(page).to have_content not_permission_message
      expect(page).not_to have_content("Process not published")
    end

    it "is available for an administrator user" do
      login_as(administrator)
      visit send(path, not_published_process)

      expect(page).to have_content("Process not published")
    end
  end

  context 'processes home page' do

    scenario 'No processes to be listed' do
      visit legislation_processes_path
      expect(page).to have_text "There aren't open processes"

      visit legislation_processes_path(filter: 'next')
      expect(page).to have_text "There aren't planned processes"

      visit legislation_processes_path(filter: 'past')
      expect(page).to have_text "There aren't past processes"
    end

    scenario 'Processes can be listed' do
      processes = create_list(:legislation_process, 3)

      visit legislation_processes_path

      processes.each do |process|
        expect(page).to have_link(process.title)
      end
    end

    scenario 'Key dates are displayed on current locale' do
      process = create(:legislation_process, proposals_phase_start_date: Date.new(2018, 01, 01),
                                             proposals_phase_end_date: Date.new(2018, 12, 01))

      visit legislation_process_path(process)

      expect(page).to have_content("Proposals")
      expect(page).to have_content("01 Jan 2018 - 01 Dec 2018")

      visit legislation_process_path(process, locale: "es")

      expect(page).to have_content("Propuestas")
      expect(page).to have_content("01 ene 2018 - 01 dic 2018")
    end

    scenario 'Filtering processes' do
      create(:legislation_process, title: "Process open")
      create(:legislation_process, :next, title: "Process next")
      create(:legislation_process, :past, title: "Process past")
      create(:legislation_process, :in_draft_phase, title: "Process in draft phase")

      visit legislation_processes_path
      expect(page).to have_content('Process open')
      expect(page).not_to have_content('Process next')
      expect(page).not_to have_content('Process past')
      expect(page).not_to have_content('Process in draft phase')

      visit legislation_processes_path(filter: 'next')
      expect(page).not_to have_content('Process open')
      expect(page).to have_content('Process next')
      expect(page).not_to have_content('Process past')

      visit legislation_processes_path(filter: 'past')
      expect(page).not_to have_content('Process open')
      expect(page).not_to have_content('Process next')
      expect(page).to have_content('Process past')
    end

    context "not published processes" do
      before do
        create(:legislation_process, title: "published")
        create(:legislation_process, :not_published, title: "not published")
        [:next, :past].each do |trait|
          create(:legislation_process, trait, title: "#{trait} published")
          create(:legislation_process, :not_published, trait, title: "#{trait} not published")
        end
      end

      it "aren't listed" do
        visit legislation_processes_path
        expect(page).not_to have_content('not published')
        expect(page).to have_content('published')

        login_as(administrator)
        visit legislation_processes_path
        expect(page).not_to have_content('not published')
        expect(page).to have_content('published')
      end

      it "aren't listed with next filter" do
        visit legislation_processes_path(filter: 'next')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('next published')

        login_as(administrator)
        visit legislation_processes_path(filter: 'next')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('next published')
      end

      it "aren't listed with past filter" do
        visit legislation_processes_path(filter: 'past')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('past published')

        login_as(administrator)
        visit legislation_processes_path(filter: 'past')
        expect(page).not_to have_content('not published')
        expect(page).to have_content('past published')
      end
    end
  end

  context 'process page' do
    context "show" do
      include_examples "not published permissions", :legislation_process_path

      scenario 'show view has document present on all phases' do
        process = create(:legislation_process)
        document = create(:document, documentable: process)
        phases = ["Debate", "Proposals", "Draft publication",
                  "Comments", "Final result publication"]

        visit legislation_process_path(process)

        phases.each do |phase|
          within(".legislation-process-list") do
            find('li', :text => "#{phase}").click_link
          end

          expect(page).to have_content(document.title)
        end
      end

      scenario 'show additional info button' do
        process = create(:legislation_process, additional_info: "Text for additional info of the process")

        visit legislation_process_path(process)

        expect(page).to have_content("Additional information")
        expect(page).to have_content("Text for additional info of the process")
      end

      scenario 'do not show additional info button if it is empty' do
        process = create(:legislation_process)

        visit legislation_process_path(process)

        expect(page).not_to have_content("Additional information")
      end

      scenario "Shows another translation when the default locale isn't available" do
        process = create(:legislation_process, title_fr: "Français")
        process.translations.where(locale: :en).first.destroy

{'fraction_non_alphanumeric': 0.09281194223592407, 'fraction_numerical': 0.006652604251176375, 'mean_word_length': 3.4056468906361688, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5553215', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3297, 'n_words': 914}