From 8c3ce7b1126be1a4ba9a77601b74a4b8a18ac48e Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Wed, 30 Oct 2024 11:34:52 +0000 Subject: [PATCH] Redirect to latest published edition --- app/controllers/content_item_controller.rb | 8 +- app/models/artefact.rb | 8 ++ .../content_item_controller_test.rb | 40 ++++-- .../get_content_by_content_id_test.rb | 116 ++++++++++++++++++ 4 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 test/integration/get_content_by_content_id_test.rb diff --git a/app/controllers/content_item_controller.rb b/app/controllers/content_item_controller.rb index e738758d5..83cd39b48 100644 --- a/app/controllers/content_item_controller.rb +++ b/app/controllers/content_item_controller.rb @@ -4,7 +4,13 @@ def by_content_id Artefact.find_by(content_id: params[:content_id]) if artefact - redirect_to edition_path(artefact.latest_edition) + if Flipflop.enabled?("design_system_publications_filter".to_sym) + redirect_url = "?title_filter=#{artefact.latest_edition.title}&assignee_filter=&content_type_filter=all&states_filter%5B%5D=draft&states_filter%5B%5D=published" + redirect_to redirect_url + else + redirect_url = "/?list=published&string_filter=#{artefact.latest_edition.slug}&user_filter=all" + redirect_to redirect_url + end else redirect_to_root_path_with_error end diff --git a/app/models/artefact.rb b/app/models/artefact.rb index c6a3ab1a3..b4fcc3921 100644 --- a/app/models/artefact.rb +++ b/app/models/artefact.rb @@ -245,6 +245,14 @@ def latest_edition .first end + def latest_published_edition + Edition + .where(panopticon_id: id) + .where(state: "published") + .order(version_number: :desc) + .first + end + def latest_edition_id edition = latest_edition edition.id.to_s if edition diff --git a/test/functional/content_item_controller_test.rb b/test/functional/content_item_controller_test.rb index 6d9e781c3..347b0dbad 100644 --- a/test/functional/content_item_controller_test.rb +++ b/test/functional/content_item_controller_test.rb @@ -4,19 +4,45 @@ class ContentItemControllerTest < ActionController::TestCase def setup login_as_stub_user @edition = FactoryBot.create(:edition) + @test_strategy = Flipflop::FeatureSet.current.test! end - should "redirect to content by content_id" do - get :by_content_id, params: { content_id: @edition.content_id } + context "design_system_publications_filter switch is enabled" do + setup do + @test_strategy.switch!(:design_system_publications_filter, true) + end - assert_redirected_to edition_path(@edition.artefact.latest_edition) + should "redirect to new design system publications page" do + get :by_content_id, params: { content_id: @edition.content_id } + + assert_routing("/", controller: "root", action: "index") + end + + should "redirect to root with error message if content_id is not found" do + get :by_content_id, params: { content_id: "#{@edition.artefact.content_id}wrong-id" } + + assert_redirected_to root_path + assert_equal "The requested content was not found", flash[:danger] + end end - should "redirect to root with error message if content_id is not found" do - get :by_content_id, params: { content_id: "#{@edition.artefact.content_id}wrong-id" } + context "design_system_publications_filter switch is disabled" do + setup do + @test_strategy.switch!(:design_system_publications_filter, false) + end - assert_redirected_to root_path - assert_equal "The requested content was not found", flash[:danger] + should "redirect to new old bootstrap ui publications page" do + get :by_content_id, params: { content_id: @edition.content_id } + + assert_routing("/", controller: "legacy_root", action: "index") + end + + should "redirect to root with error message if content_id is not found" do + get :by_content_id, params: { content_id: "#{@edition.artefact.content_id}wrong-id" } + + assert_redirected_to root_path + assert_equal "The requested content was not found", flash[:danger] + end end should "redirect to root with error message if any error" do diff --git a/test/integration/get_content_by_content_id_test.rb b/test/integration/get_content_by_content_id_test.rb new file mode 100644 index 000000000..2ffdc9e0b --- /dev/null +++ b/test/integration/get_content_by_content_id_test.rb @@ -0,0 +1,116 @@ +require "integration_test_helper" + +class GetContentByContentIdTest < IntegrationTest + def setup + login_as_stub_user + @artefact = FactoryBot.create(:artefact, name: "browser extension test") + @test_strategy = Flipflop::FeatureSet.current.test! + end + + context "design_system_publications_filter switch is enabled" do + setup do + @test_strategy.switch!(:design_system_publications_filter, true) + end + + should "show only one edition when content item has only one draft edition" do + create_draft_edition + visit "by-content-id/#{@draft_edition.content_id}" + + assert_content("1 document(s)") + assert page.has_content?("answer edition") + end + + should "show only one edition when content item has only one published edition" do + create_published_edition + visit "by-content-id/#{@published_edition.content_id}" + + assert_content("1 document(s)") + assert page.has_content?("answer edition") + end + + should "show two editions when content item has one published and one draft edition" do + create_published_edition + create_draft_edition + visit "by-content-id/#{@published_edition.content_id}" + + assert_content("2 document(s)") + assert page.has_content?("answer edition") + end + end + + context "design_system_publications_filter switch is disabled" do + setup do + @test_strategy.switch!(:design_system_publications_filter, false) + end + + context "Content items that have only one edition, in the draft state" do + should "show main list as empty and 'filter by status' counts as all zero except for 'Drafts'" do + filter_by_status = ["Filter by Status", "Drafts 1", "In review 0", "Amends needed 0", "Out for fact check 0", "Fact check received 0", "Ready 0", "Scheduled 0", "Published 0", "Archived 0"] + create_draft_edition + visit "by-content-id/#{@draft_edition.content_id}" + + sidebar_filter_by_status = find_all(".nav-list")[0] + within sidebar_filter_by_status do + statuses = find_all("li") + statuses.each do |status| + assert filter_by_status.include?(status.text) + end + end + + assert page.has_no_css?("#publication-list-container table tbody tr") + assert page.has_no_content?("answer edition") + end + end + + context "Content items that have only one edition, in the published state " do + should "show main list with a single edition and a 'Create new edition' button and 'filter by status' counts as all zero except for 'Published'" do + filter_by_status = ["Filter by Status", "Drafts 0", "In review 0", "Amends needed 0", "Out for fact check 0", "Fact check received 0", "Ready 0", "Scheduled 0", "Published 1", "Archived 0"] + create_published_edition + visit "by-content-id/#{@published_edition.content_id}" + + sidebar_filter_by_status = find_all(".nav-list")[0] + within sidebar_filter_by_status do + statuses = find_all("li") + statuses.each do |status| + assert filter_by_status.include?(status.text) + end + end + + assert page.has_css?("#publication-list-container table tbody tr") + assert page.has_content?("answer edition") + assert page.has_link?("Create new edition") + end + end + + context "Content items that have one edition in the published state and one item in draft state " do + should "show main list with a single edition and a 'Edit newer edition' button and 'filter by status' counts as all zero except for 'Published' and 'Draft'" do + filter_by_status = ["Filter by Status", "Drafts 1", "In review 0", "Amends needed 0", "Out for fact check 0", "Fact check received 0", "Ready 0", "Scheduled 0", "Published 1", "Archived 0"] + create_published_edition + create_draft_edition + visit "by-content-id/#{@published_edition.content_id}" + + sidebar_filter_by_status = find_all(".nav-list")[0] + within sidebar_filter_by_status do + statuses = find_all("li") + statuses.each do |status| + assert filter_by_status.include?(status.text) + end + end + + assert page.has_css?("#publication-list-container table tbody tr") + assert page.has_content?("answer edition") + assert page.has_link?("Edit newer edition") + end + end + end + +private + + def create_draft_edition + @draft_edition = FactoryBot.create(:guide_edition, state: :draft, title: "answer edition", panopticon_id: @artefact.id, slug: "browser-extension-test") + end + + def create_published_edition + @published_edition = FactoryBot.create(:guide_edition, state: :published, title: "answer edition", panopticon_id: @artefact.id, slug: "browser-extension-test") + end +end