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

12.4 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 "Projects", "creation",
               :js do
  shared_let(:project_custom_field_section) { create(:project_custom_field_section, name: "Section A") }

  current_user { create(:admin) }

  shared_let(:project) { create(:project, name: "Foo project", identifier: "foo-project") }

  let(:projects_page) { Pages::Projects::Index.new }

  before do
    projects_page.visit!
  end

  context "with the button on the toolbar items" do
    it "can navigate to the create project page" do
      projects_page.create_new_workspace

      expect(page).to have_heading "New project"

      expect(page).to have_current_path(new_project_path)
    end
  end

  it "can create a project" do
    projects_page.create_new_workspace

    expect(page).to have_heading "New project"

    fill_in "Name", with: "Foo bar"
    click_on "Create"

    expect_and_dismiss_flash type: :success, message: "Successful creation."

    expect(page).to have_current_path /\/projects\/foo-bar\/?/
    expect(page).to have_content "Foo bar"
  end

  it "does not create a project with an already existing identifier" do
    projects_page.create_new_workspace

    expect(page).to have_heading "New project"

    fill_in "Name", with: "Foo project"
    click_on "Create"

    expect_and_dismiss_flash type: :success, message: "Successful creation."

    expect(page).to have_current_path /\/projects\/foo-project-1\/?/

    project = Project.last
    expect(project.identifier).to eq "foo-project-1"
  end

  context "with a multi-select list custom field" do
    shared_let(:list_custom_field) do
      create(:list_project_custom_field,
             name: "List CF",
             is_required: true,
             multi_value: true,
             project_custom_field_section:)
    end
    let(:list_field) do
      FormFields::SelectFormField.new(
        list_custom_field,
        selector: "[data-qa-field-name='#{list_custom_field.attribute_name(:kebab_case)}'"
      )
    end

    it "can create a project" do
      projects_page.create_new_workspace

      expect(page).to have_heading "New project"

      fill_in "Name", with: "Foo bar"

      expect(page).to have_combo_box "List CF *"
      list_field.select_option "A", "B"

      click_on "Create"

      expect_and_dismiss_flash type: :success, message: "Successful creation."

      expect(page).to have_current_path /\/projects\/foo-bar\/?/
      expect(page).to have_content "Foo bar"

      project = Project.last
      expect(project.name).to eq "Foo bar"
      cvs = project.custom_value_for(list_custom_field)
      expect(cvs.count).to eq 2
      expect(cvs.map(&:typed_value)).to contain_exactly "A", "B"
    end
  end

  context "with a multi-select version custom field" do
    include_context "ng-select-autocomplete helpers"

    shared_let(:public_project) do
      create(:project, name: "Public Pr", identifier: "public-pr", public: true)
    end

    shared_let(:versions) do
      [
        create(:version, project:, name: "Ringbo 1.0", sharing: "system"),
        create(:version, project: public_project, name: "Ringbo 2.0", sharing: "system")
      ]
    end

    shared_let(:version_custom_field) do
      create(:version_project_custom_field,
             name: "Version CF",
             is_required: true,
             multi_value: true,
             project_custom_field_section:)
    end

    let(:version_field) do
      FormFields::SelectFormField.new(
        version_custom_field,
        selector: "[data-qa-field-name='#{version_custom_field.attribute_name(:kebab_case)}'"
      )
    end

    it "can create a project" do
      projects_page.create_new_workspace

      expect(page).to have_heading "New project"

      fill_in "Name", with: "Foo bar"

      expect(page).to have_combo_box "Version CF *"

      # expect the versions are grouped by the project name
      version_field.expect_option(versions.first.name, grouping: project.name)
      version_field.expect_option(versions.last.name, grouping: public_project.name)

      version_field.select_option(versions.first.name, versions.last.name)

      click_on "Create"

      expect_and_dismiss_flash type: :success, message: "Successful creation."

      expect(page).to have_current_path /\/projects\/foo-bar\/?/
      expect(page).to have_content "Foo bar"

      project = Project.last
      expect(project.name).to eq "Foo bar"

      typed_values = project.custom_value_for(version_custom_field).map(&:typed_value)
      expect(typed_values).to eq versions
    end
  end

  it "hides the active field and the identifier" do
    visit new_project_path

    expect(page).to have_heading "New project"

    expect(page).to have_no_content "Active"
    expect(page).to have_no_content "Identifier"
  end

  context "with optional and required custom fields" do
    shared_let(:optional_custom_field) do
      create(:project_custom_field, name: "Optional Foo",
                                    field_format: "string",
                                    is_for_all: true,
                                    project_custom_field_section:)
    end
    shared_let(:required_custom_field) do
      create(:project_custom_field, name: "Required Foo",
                                    field_format: "string",
                                    is_for_all: true,
                                    is_required: true,
                                    project_custom_field_section:)
    end

    context "with required custom fields" do
      shared_let(:required_user_custom_field) do
        create(:user_project_custom_field, name: "Required User",
                                           is_for_all: true,
                                           is_required: true,
                                           project_custom_field_section:)
      end

      it "renders required custom fields for new" do
        visit new_project_path

        expect(page).to have_heading "New project"

        expect(page).to have_field "Required Foo", required: true
        expect(page).to have_field "Required User *" # FIXME required: true
        expect(page).to have_no_field "Optional Foo"
      end
    end

    context "with correct validations" do
      before do
        visit new_project_path
      end

      it "requires the required custom field" do
        expect(page).to have_heading "New project"

        click_on "Create"

        expect_and_dismiss_flash type: :error, message: /^Creation failed/

        expect(page).to have_field "Required Foo", validation_error: "can't be blank."
      end
    end

    context "with correct custom field activation" do
      shared_let(:unused_custom_field) do
        create(:project_custom_field, name: "Unused Foo",
                                      field_format: "string",
                                      project_custom_field_section:)
      end

      before do
        visit new_project_path

        expect(page).to have_heading "New project" # rubocop:disable RSpec/ExpectInHook

        fill_in "Name", with: "Foo bar"
        fill_in "Required Foo", with: "Required value"
      end

      it "enables custom fields with provided values for this project" do
        click_on "Create"

        expect_and_dismiss_flash type: :success, message: "Successful creation."

        expect(page).to have_current_path /\/projects\/foo-bar\/?/

        project = Project.last

        # unused custom field should not be activated
        expect(project.project_custom_field_ids).to contain_exactly(
          required_custom_field.id
        )
      end

      context "with correct handling of default values" do
        shared_let(:custom_field_with_default_value) do
          create(:project_custom_field, name: "Foo with default value",
                                        field_format: "string",
                                        is_required: true,
                                        default_value: "Default value",
                                        project_custom_field_section:)
        end

        it "enables custom fields with default values if not set to blank explicitly" do
          # don't touch the default value
          click_on "Create"

          expect_and_dismiss_flash type: :success, message: "Successful creation."

          expect(page).to have_current_path /\/projects\/foo-bar\/?/

          project = Project.last

          # custom_field_with_default_value should be activated and contain the default value
          expect(project.project_custom_field_ids).to contain_exactly(
            custom_field_with_default_value.id, required_custom_field.id
          )

          expect(project.custom_value_for(custom_field_with_default_value).value).to eq("Default value")
        end

        it "does enable custom fields with default values if overwritten with a new value" do
          fill_in "Foo with default value", with: "foo"

          click_on "Create"

          expect(page).to have_current_path /\/projects\/foo-bar\/?/

          expect_and_dismiss_flash type: :success, message: "Successful creation."

          project = Project.last

          # custom_field_with_default_value should be activated and contain the overwritten value
          expect(project.project_custom_field_ids).to contain_exactly(
            custom_field_with_default_value.id, required_custom_field.id
          )

          expect(project.custom_value_for(custom_field_with_default_value).value).to eq("foo")
        end
      end

      context "with correct handling of invisible values" do
        shared_let(:invisible_field) do
          create(:string_project_custom_field, name: "Text for Admins only",
                                               is_required: true,
                                               admin_only: true,
                                               project_custom_field_section:)
        end

        context "with an admin user" do
          it "shows invisible fields in the form and allows their activation" do
            expect(page).to have_content "Text for Admins only"

            fill_in "Text for Admins only", with: "foo"

            click_on "Create"

            expect_and_dismiss_flash type: :success, message: "Successful creation."

            expect(page).to have_current_path /\/projects\/foo-bar\/?/

            project = Project.last

            expect(project.project_custom_field_ids).to contain_exactly(
              required_custom_field.id, invisible_field.id
            )

            expect(project.custom_value_for(invisible_field).typed_value).to eq("foo")
          end
        end

        context "with a non-admin user" do
          current_user { create(:user, global_permissions: %i[add_project]) }

          it "does not show invisible fields in the form and thus not activates the invisible field" do
            pending "Admin-only project attributes currently prevent users from creating projects (OP#64479)"

            expect(page).to have_no_content "Text for Admins only"

            click_on "Create"

            expect_and_dismiss_flash type: :success, message: "Successful creation."

            expect(page).to have_current_path /\/projects\/foo-bar\/?/

            project = Project.last

            expect(project.project_custom_field_ids).to contain_exactly(
              required_custom_field.id
            )
          end
        end
      end
    end
  end
end