translate module_item_id query parameters in course copy

test plan:
* create a course with an assignment added as a module item
* follow the link to the assignment from the modules page
* note the url with a "?module_item_id=X"
* create html content in the course with a link to that url
* copy the course
* it should translate the link so the module_item_id points
 to the id of the new module item

closes #ADMIN-1289

Change-Id: Iabac7f65751d55124349c5e99e361a11cf1ef9b1
Reviewed-on: https://gerrit.instructure.com/159035
Reviewed-by: Jeremy Stanley <jeremy@instructure.com>
Tested-by: Jenkins
QA-Review: Carl Kibler <ckibler@instructure.com>
Product-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
James Williams 2018-07-30 07:53:53 -06:00
parent 9453e535e3
commit b8a5874f5c
3 changed files with 39 additions and 2 deletions

View File

@ -65,7 +65,8 @@ module Importers
scope = context.send(type).scope
if scope.klass.columns_hash['migration_id']
if object_id = scope.where(migration_id: migration_id).limit(1).pluck(:id).first
link[:new_value] = "#{context_path}/#{type_for_url}/#{object_id}#{link[:query]}"
query = resolve_module_item_query(context, link[:query])
link[:new_value] = "#{context_path}/#{type_for_url}/#{object_id}#{query}"
end
end
end
@ -99,6 +100,17 @@ module Importers
end
end
def resolve_module_item_query(context, query)
return query unless query&.include?("module_item_id=")
original_param = query.sub("?", "").split("&").detect{|p| p.include?("module_item_id=")}
mig_id = original_param.split("=").last
tag = context.context_module_tags.where(:migration_id => mig_id).first
return query unless tag
new_param = "module_item_id=#{tag.id}"
query.sub(original_param, new_param)
end
def missing_relative_file_url(rel_path)
# the rel_path should already be escaped
File.join(URI::escape("#{context_path}/file_contents/#{Folder.root_folders(context).first.name}"), rel_path.gsub(" ", "%20"))

View File

@ -253,7 +253,8 @@ module CCHelper
# for all other types,
# create a migration id for the object, and use that as the new link
migration_id = @key_generator.create_key(obj)
new_url = "#{OBJECT_TOKEN}/#{match.type}/#{migration_id}#{match.query}"
query = translate_module_item_query(match.query)
new_url = "#{OBJECT_TOKEN}/#{match.type}/#{migration_id}#{query}"
end
elsif match.obj_id
new_url = "#{COURSE_TOKEN}/#{match.type}/#{match.obj_id}#{match.rest}"
@ -270,6 +271,14 @@ module CCHelper
@url_prefix += ":#{port}" if !host.include?(':') && port.present?
end
def translate_module_item_query(query)
return query unless query&.include?("module_item_id=")
original_param = query.sub("?", "").split("&").detect{|p| p.include?("module_item_id=")}
tag_id = original_param.split("=").last
new_param = "module_item_id=#{@key_generator.create_key("content_tag_#{tag_id}")}"
query.sub(original_param, new_param)
end
attr_reader :course, :user
def html_page(html, title, meta_fields={})

View File

@ -179,6 +179,22 @@ describe ContentMigration do
expect(page_to.body).to eq body % [@copy_to.id, tag_to.id]
end
it "should translate links to assignments with module item id" do
mod1 = @copy_from.context_modules.create!(:name => "some module")
asmnt1 = @copy_from.assignments.create!(:title => "some assignment")
tag = mod1.add_item({:id => asmnt1.id, :type => 'assignment', :indent => 1})
body = %{<p>Link to module item: <a href="/courses/%s/assignments/%s?module_item_id=%s">some assignment</a></p>}
page = @copy_from.wiki_pages.create!(:title => "some page", :body => body % [@copy_from.id, asmnt1.id, tag.id])
run_course_copy
mod1_to = @copy_to.context_modules.where(migration_id: mig_id(mod1)).first
asmnt_to = @copy_to.assignments.where(migration_id: mig_id(asmnt1)).first
tag_to = mod1_to.content_tags.first
page_to = @copy_to.wiki_pages.where(migration_id: mig_id(page)).first
expect(page_to.body).to eq body % [@copy_to.id, asmnt_to.id, tag_to.id]
end
it "should translate links to modules in quiz content" do
skip unless Qti.qti_enabled?