Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copying page with descendants to a different language #2243

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ def copy_children_to(new_parent)
next if child == new_parent

new_child = Page.copy(child, {
parent_id: new_parent.id,
language_id: new_parent.language_id,
language_code: new_parent.language_code,
})
Expand Down
128 changes: 96 additions & 32 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class AnotherUrlPathClass; end
before do
create(:alchemy_page, :public, :locked, locked_by: 53) # This page must not be part of the collection
allow(user.class).to receive(:primary_key)
.and_return("id")
.and_return("id")
end

it "should return the correct page collection blocked by a certain user" do
Expand All @@ -308,7 +308,7 @@ class AnotherUrlPathClass; end

before do
allow(user.class).to receive(:primary_key)
.and_return("user_id")
.and_return("user_id")
end

it "should return the correct page collection blocked by a certain user" do
Expand Down Expand Up @@ -423,9 +423,9 @@ class AnotherUrlPathClass; end
before do
page = create(:alchemy_page)
allow(page).to receive(:definition).and_return({
"name" => "standard",
"elements" => ["headline"],
"autogenerate" => ["headline"],
"name" => "standard",
"elements" => ["headline"],
"autogenerate" => ["headline"],
})
end

Expand Down Expand Up @@ -663,22 +663,22 @@ class AnotherUrlPathClass; end

before do
allow(Element).to receive(:definitions).and_return([
{
"name" => "column_headline",
"amount" => 3,
"contents" => [{ "name" => "headline", "type" => "EssenceText" }],
},
{
"name" => "unique_headline",
"unique" => true,
"amount" => 3,
"contents" => [{ "name" => "headline", "type" => "EssenceText" }],
},
{
"name" => "column_headline",
"amount" => 3,
"contents" => [{ "name" => "headline", "type" => "EssenceText" }],
},
{
"name" => "unique_headline",
"unique" => true,
"amount" => 3,
"contents" => [{ "name" => "headline", "type" => "EssenceText" }],
},
])
allow(PageLayout).to receive(:get).and_return({
"name" => "columns",
"elements" => ["column_headline", "unique_headline"],
"autogenerate" => ["unique_headline", "column_headline", "column_headline", "column_headline"],
"name" => "columns",
"elements" => ["column_headline", "unique_headline"],
"autogenerate" => ["unique_headline", "column_headline", "column_headline", "column_headline"],
})
end

Expand Down Expand Up @@ -1047,9 +1047,9 @@ class AnotherUrlPathClass; end
context "with existing public child" do
let!(:first_public_child) do
create :alchemy_page, :public,
name: "First public child",
language: language,
parent: language_root
name: "First public child",
language: language,
parent: language_root
end

it "should return first_public_child" do
Expand Down Expand Up @@ -1110,6 +1110,70 @@ class AnotherUrlPathClass; end
end
end

describe "#copy_children_to" do
subject { source_page.copy_children_to new_parent }

let(:source_page) do
create(
:alchemy_page,
:public,
children: [
build(:alchemy_page),
build(:alchemy_page, name: "child with children", children: [build(:alchemy_page)]),
]
)
end

let(:new_parent) { create :alchemy_page, :public }

it "copies children and their descendents under new_parent" do
expect { subject }.to change { new_parent.children.count }.from(0).to 2

source_page.children.each do |child|
expect(new_parent.children.where(title: child.title).count).to eq 1
end

source_page_grandchildren = source_page.children.find_by_title("child with children").children
new_parent_grandchildren = new_parent.children.find_by_title("child with children").children

source_page_grandchildren.each do |grandchild|
expect(new_parent_grandchildren.where(title: grandchild.title).count).to eq 1
end
end

context "when copying to a new_parent in a different language tree" do
let(:new_parent) { create :alchemy_page, :public, language: klingon }

it "copies children and their descendents under new_parent" do
expect { subject }.to change { new_parent.children.count }.from(0).to 2

source_page.children.each do |child|
expect(new_parent.children.where(title: child.title).count).to eq 1
end

source_page_grandchildren = source_page.children.find_by_title("child with children").children
new_parent_grandchildren = new_parent.children.find_by_title("child with children").children

source_page_grandchildren.each do |grandchild|
expect(new_parent_grandchildren.where(title: grandchild.title).count).to eq 1
end
end
end
end

def copy_children_to(new_parent)
children.each do |child|
next if child == new_parent

new_child = Page.copy(child, {
language_id: new_parent.language_id,
language_code: new_parent.language_code,
})
new_child.move_to_child_of(new_parent)
child.copy_children_to(new_child) unless child.children.blank?
end
end

describe "#definition" do
context "if the page layout could not be found in the definition file" do
let(:page) { build_stubbed(:alchemy_page, page_layout: "notexisting") }
Expand Down Expand Up @@ -1163,11 +1227,11 @@ class AnotherUrlPathClass; end

it "should copy the source page with the given name to the new parent" do
expect(Page).to receive(:copy).with(source, {
parent: new_parent,
language: new_parent.language,
name: page_name,
title: page_name,
})
parent: new_parent,
language: new_parent.language,
name: page_name,
title: page_name,
})
subject
end

Expand All @@ -1191,11 +1255,11 @@ class AnotherUrlPathClass; end

it "copies the source page with the given name" do
expect(Page).to receive(:copy).with(source, {
parent: nil,
language: nil,
name: page_name,
title: page_name,
})
parent: nil,
language: nil,
name: page_name,
title: page_name,
})
subject
end
end
Expand Down