summaryrefslogtreecommitdiff
path: root/test/controllers/page_translations_controller_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/controllers/page_translations_controller_test.rb')
-rw-r--r--test/controllers/page_translations_controller_test.rb97
1 files changed, 97 insertions, 0 deletions
diff --git a/test/controllers/page_translations_controller_test.rb b/test/controllers/page_translations_controller_test.rb
new file mode 100644
index 0000000..8c899fd
--- /dev/null
+++ b/test/controllers/page_translations_controller_test.rb
@@ -0,0 +1,97 @@
1require 'test_helper'
2
3class PageTranslationsControllerTest < ActionController::TestCase
4 test "index lists the default locale's existing translation and flags a missing one" do
5 login_as :quentin
6 node = Node.root.children.create!(:slug => "translations_index_test")
7 node.publish_draft!
8
9 get :index, params: { :node_id => node.id }
10
11 assert_response :success
12 assert_equal [:en], assigns(:translations).map { |t| t[:locale] }
13 assert_equal false, assigns(:translations).first[:exists]
14 end
15
16 test "update creates a first-time translation on a fresh draft, leaving head untouched" do
17 login_as :quentin
18 node = Node.root.children.create!(:slug => "translations_create_test")
19 Globalize.with_locale(:de) { node.draft.update!(:title => "Deutscher Titel") }
20 node.publish_draft!
21 node.lock_for_editing!(users(:quentin))
22
23 patch :update, params: { :node_id => node.id, :translation_locale => "en", :page => { :title => "English Title" } }
24
25 node.reload
26 assert_not_nil node.draft
27 assert_includes node.draft.translated_locales, :en
28 assert_not_includes node.head.translated_locales, :en
29 end
30
31 test "no route exists for the default locale under the translations resource" do
32 login_as :quentin
33 node = Node.root.children.create!(:slug => "translations_route_constraint_test")
34
35 assert_raises(ActionController::UrlGenerationError) do
36 patch :update, params: { :node_id => node.id, :translation_locale => "de", :page => { :title => "x" } }
37 end
38 end
39
40 test "update with Save + Unlock + Exit unlocks the node and redirects to nodes#show" do
41 login_as :quentin
42 node = Node.root.children.create!(:slug => "translations_exit_test")
43 node.lock_for_editing!(users(:quentin))
44
45 patch :update, params: { :node_id => node.id, :translation_locale => "en", :page => { :title => "x" }, :commit => "Save + Unlock + Exit" }
46
47 assert_nil node.reload.lock_owner
48 assert_redirected_to node_path(node)
49 end
50
51 test "update saves the translation onto the draft" do
52 login_as :quentin
53 node = Node.root.children.create!(:slug => "translations_update_test")
54 Globalize.with_locale(:en) { node.draft.update!(:title => "Original") }
55 node.lock_for_editing!(users(:quentin))
56
57 patch :update, params: { :node_id => node.id, :translation_locale => "en", :page => { :title => "Revised" } }
58
59 assert_equal "Revised", Globalize.with_locale(:en) { node.draft.reload.title }
60 end
61
62 test "destroy refuses to remove the only remaining translation" do
63 login_as :quentin
64 node = Node.root.children.create!(:slug => "translations_destroy_last_test")
65 Globalize.with_locale(:en) { node.draft.update!(:title => "Only translation") }
66 node.draft.translations.where(:locale => :de).delete_all
67
68 delete :destroy, params: { :node_id => node.id, :translation_locale => "en" }
69
70 assert_equal "Can't remove the only remaining translation.", flash[:error]
71 end
72
73 test "destroy is a safe no-op, not a false success, when the translation doesn't exist" do
74 login_as :quentin
75 node = Node.root.children.create!(:slug => "translations_destroy_missing_test")
76
77 delete :destroy, params: { :node_id => node.id, :translation_locale => "en" }
78
79 assert_match(/No EN translation exists/, flash[:error])
80 end
81
82 test "autosave writes the translation without creating a new revision or touching the draft" do
83 login_as :quentin
84 node = Node.root.children.create!(:slug => "translations_autosave_test")
85 node.publish_draft!
86 node.lock_for_editing!(users(:quentin))
87 page_count_before = node.pages.count
88
89 put :autosave, params: { :node_id => node.id, :translation_locale => "en", :page => { :title => "in progress" } }
90
91 assert_response :success
92 node.reload
93 assert_not_nil node.autosave
94 assert_equal page_count_before, node.pages.count
95 assert_equal "in progress", Globalize.with_locale(:en) { node.autosave.title }
96 end
97end