Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

5.9 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

require "spec_helper"

RSpec.describe "Project Custom Field Mappings", :js do
  shared_let(:admin) { create(:admin) }
  shared_let(:non_admin) { create(:user) }
  shared_let(:project) { create(:project) }
  shared_let(:archived_project) { create(:project, active: false) }
  shared_let(:project_custom_field) { create(:project_custom_field) }
  shared_let(:project_custom_field_mapping) { create(:project_custom_field_project_mapping, project_custom_field:, project:) }

  shared_let(:archived_project_custom_field_mapping) do
    create(:project_custom_field_project_mapping, project_custom_field:, project: archived_project)
  end

  let(:project_custom_field_mappings_page) { Pages::Admin::Settings::ProjectCustomFields::ProjectMappings.new }

  context "with insufficient permissions" do
    it "is not accessible" do
      login_as(non_admin)
      visit project_mappings_admin_settings_project_custom_field_path(project_custom_field)

      expect(page).to have_text("You are not authorized to access this page.")
    end
  end

  context "with sufficient permissions" do
    before do
      login_as(admin)
      visit project_mappings_admin_settings_project_custom_field_path(project_custom_field)
    end

    it "renders a list of projects linked to the custom field" do
      aggregate_failures "shows a correct breadcrumb menu" do
        within ".PageHeader-breadcrumbs" do
          expect(page).to have_link("Administration")
          expect(page).to have_link("Projects")
          expect(page).to have_link("Project attributes")
          expect(page).to have_text(project_custom_field.name)
        end
      end

      aggregate_failures "shows tab navigation" do
        within_test_selector("project_attribute_detail_header") do
          expect(page).to have_link("Details")
          expect(page).to have_link("Projects")
        end
      end

      aggregate_failures "shows the correct project mappings" do
        within "#project-table" do
          expect(page).to have_text(project.name)
          expect(page).to have_text(archived_project.name)

          within("tr#settings-project-custom-fields-project-custom-field-mapping-row-component-project-#{archived_project.id}") do
            expect(page.find(".buttons")).not_to have_test_selector("project-list-row--action-menu")
          end
        end
      end
    end

    it "shows an error in the dialog when no project is selected before adding" do
      create(:project)
      expect(page).to have_no_css("dialog")
      click_on "Add projects"

      page.within("dialog") do
        click_on "Add"

        expect(page).to have_text("Please select a project.")
      end
    end

    it "allows linking a project to a custom field" do
      project = create(:project)
      subproject = create(:project, parent: project)
      click_on "Add projects"

      within_test_selector("new-custom-field-projects-modal") do
        autocompleter = page.find(".op-project-autocompleter")
        autocompleter.fill_in with: project.name

        expect(page).to have_no_text(archived_project.name)

        find(".ng-option-label", text: project.name).click
        check "Include sub-projects"

        click_on "Add"
      end

      expect(page).to have_text(project.name)
      expect(page).to have_text(subproject.name)

      aggregate_failures "pagination links maintain the correct url" do
        project_custom_field_mappings_page.expect_page_sizes(model: project_custom_field)
      end
    end

    it "allows unlinking a project from a custom field", with_settings: { per_page_options: "2,5" } do
      projects = create_list(:project, 4)
      projects.each { |project| create(:project_custom_field_project_mapping, project_custom_field:, project:) }

      current_page = 3
      visit project_mappings_admin_settings_project_custom_field_path(project_custom_field, page: current_page)

      project = project_custom_field_mappings_page.project_in_first_row
      project_custom_field_mappings_page.click_menu_item_of("Remove from project", project)

      expect(page).to have_no_text(project.name)

      aggregate_failures "pagination links maintain the correct url after unlinking is done" do
        project_custom_field_mappings_page.expect_page_links(model: project_custom_field, current_page:)
        project_custom_field_mappings_page.expect_current_page_number(current_page)
      end
    end

    context "and the project custom field is required" do
      shared_let(:project_custom_field) { create(:project_custom_field, is_required: true) }

      it "renders a blank slate" do
        expect(page).to have_text("Required in all projects")
        expect(page).not_to have_test_selector("add-projects-sub-header")
      end
    end
  end
end