From 0818a3057b0a91e422158d828026c941b4e10622 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Thu, 25 Jun 2026 17:51:45 +0200 Subject: Rails 5.2 test updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Rename test/functional → test/controllers, test/unit → test/models - Remove test/performance/browsing_test.rb (performance_test_help removed) - Fix use_transactional_fixtures → use_transactional_tests - Remove use_instantiated_fixtures (removed in Rails 5) - Fix ActiveRecord::Fixtures → FixtureSet - Fix controller test params syntax: add params: {} wrapper throughout - Fix assert_select targets for aggregator test - Fix test_update_a_draft_with_changing_the_template: draft → head - Add test_node.reload after children.create! (awesome_nested_set bug) - Add before/after count pattern for create tests (transactional isolation) - Known failures: 5 tests affected by Rails 5 transactional test isolation --- test/models/page_test.rb | 146 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 test/models/page_test.rb (limited to 'test/models/page_test.rb') diff --git a/test/models/page_test.rb b/test/models/page_test.rb new file mode 100644 index 0000000..8b34399 --- /dev/null +++ b/test/models/page_test.rb @@ -0,0 +1,146 @@ +require 'test_helper' + +class PageTest < ActiveSupport::TestCase + + def setup + @user1 = User.create :login => 'demo', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar' + @user2 = User.create :login => 'show', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar' + end + + def test_aggregation + # Create two nodes and move them beneath the root node + n1 = Node.root.children.create! :slug => "one" + n2 = Node.root.children.create! :slug => "two" + + # get the drafts and assign a user to it + assert_not_nil d1 = n1.find_or_create_draft( @user1 ) + assert_not_nil d3 = n2.find_or_create_draft( @user1 ) + + # tag and double publish so we have 4 pages tagged with "update" + d1.tag_list = "update" + d1.save + n1.publish_draft! + + d2 = n1.find_or_create_draft @user1 + n1.publish_draft! + + + d3.tag_list = "update, pressemitteilung" + d3.save + n2.publish_draft! + + d4 = n2.find_or_create_draft @user1 + n2.publish_draft! + + # Set up two options hashes for the assertions + options1 = { + :tags => "update" + } + + options2 = { + :tags => "update, pressemitteilung" + } + + assert_equal 2, Page.aggregate( options1 ).length + assert_equal 1, Page.aggregate( options2 ).length + assert_equal 4, Page.tagged_with( "update" ).length + assert_equal [d2.id, d4.id], Page.aggregate( options1 ).map {|x| x.id} + end + + def test_before_save_rewrite_links_in_body + n = Node.root.children.create :slug => "link_test" + d = n.find_or_create_draft @user1 + + before = "

Hello World

\n" \ + "Linkme" + + after = "

Hello World

\n" \ + "Linkme" + + I18n.locale = :de + + d.body = before + d.save! + + assert_equal after, d.body + end + + def test_before_save_rewrite_links_in_body_if_no_locale_prefix_present + n = Node.root.children.create :slug => "link_test" + d = n.find_or_create_draft @user1 + + before = "

Hello World

\n" \ + "Linkme" + + after = "

Hello World

\n" \ + "Linkme" + + I18n.locale = :de + + d.body = before + d.save + + assert_equal after, d.body + end + + def test_before_save_rewrite_links_skips_on_external_links + n = Node.root.children.create :slug => "link_test" + d = n.find_or_create_draft @user1 + + before = "

Hello World

\n" \ + "Linkme" + + after = "

Hello World

\n" \ + "Linkme" + + I18n.locale = :de + + d.body = before + d.save + + assert_equal after, d.body + end + + def test_find_with_outdated_translations + Node.delete_all + Page.delete_all + I18n.locale = :de + + assert_not_nil page = Page.create!( :title => "Hallo" ) + page.reload + assert_equal 1, page.translations.size + assert_equal [], Page.find_with_outdated_translations + + I18n.locale = :en + page.title = "Hello" + page.save + + assert_equal 2, page.translations.size + assert_equal 0, Page.find_with_outdated_translations.size + + english = page.translations.select {|x| x.locale == :en}.first + Page::Translation.record_timestamps = false + english.update_attributes(:updated_at => (Time.now+25.hours)) + Page::Translation.record_timestamps = true + assert_equal 1, Page.find_with_outdated_translations.count + + I18n.locale = :de + page2 = Page.create!( :title => "Hallo2" ) + I18n.locale = :en + page2.title = "Hello2" + page2.save! + + assert_equal 0, Page.find_with_outdated_translations(:delta_time => 23.days).count + assert_equal 1, Page.find_with_outdated_translations(:delta_time => 23.minutes).count + assert_equal 2, Page.count + end + + test "pages under /updates node get the update template assigned" do + Node.root.descendants.delete_all + updates = Node.root.children.create!( :slug => "updates" ) + updates2009 = updates.children.create!( :slug => "2009" ) + update = updates2009.children.create!( :slug => "my-first-update" ) + assert_equal "update", update.draft.template_name + end + +end -- cgit v1.3