From 144b18c5db61c53028177680295f3fc9d4e8711d Mon Sep 17 00:00:00 2001 From: hukl Date: Sat, 18 Jul 2009 18:17:06 +0200 Subject: Test::Unit Fixtures can't handle globalize2's translated attributes which is why its impossible to set translated attributes via fixtures. Therefor I removed the page fixtures entirely and and made sure that a title is set to "Untitled" when it is not specified otherwise. If a new node is created, its initial draft has "Untitled" set as title automatically. Modified tests accordingly --- app/models/node.rb | 2 +- app/models/page.rb | 22 +++++++---- test/fixtures/pages.yml | 64 +++++++++++++++++--------------- test/functional/nodes_controller_test.rb | 4 ++ test/unit/node_test.rb | 4 ++ test/unit/page_test.rb | 6 +-- 6 files changed, 61 insertions(+), 41 deletions(-) diff --git a/app/models/node.rb b/app/models/node.rb index 647a86a..2a82ba8 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -67,7 +67,7 @@ class Node < ActiveRecord::Base end def create_new_draft user - empty_page = self.pages.create + empty_page = self.pages.create! empty_page.user = user empty_page.save diff --git a/app/models/page.rb b/app/models/page.rb index 49ac586..0a978ab 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -30,7 +30,8 @@ class Page < ActiveRecord::Base belongs_to :user # Filter - before_save :rewrite_links_in_body + before_create :set_page_title + before_save :rewrite_links_in_body # Security attr_accessible :title, :abstract, :body, :template_name, :published_at @@ -168,16 +169,21 @@ class Page < ActiveRecord::Base private + def set_page_title + if title.nil? + title = "Untitled" + end + end + def rewrite_links_in_body begin if self.body - tmp_body = "
#{self.body}
" - xml_string = XML::Parser.string( tmp_body ) - xml_doc = xml_string.parse - links = xml_doc.find("//a[not(starts-with(@href, 'http://'))]") - - locales = I18n.available_locales.reject {|l| l == :root} - + tmp_body = "
#{self.body}
" + xml_string = XML::Parser.string( tmp_body ) + xml_doc = xml_string.parse + links = xml_doc.find("//a[not(starts-with(@href, 'http://'))]") + locales = I18n.available_locales.reject {|l| l == :root} + links.each do |link| unless locales.include? link[:href].slice(1,2).to_sym link[:href] = link[:href].sub(/^\//, "/#{I18n.locale}/") diff --git a/test/fixtures/pages.yml b/test/fixtures/pages.yml index 271c494..686f173 100644 --- a/test/fixtures/pages.yml +++ b/test/fixtures/pages.yml @@ -1,31 +1,37 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: - id: 1 - revision: 1 - node_id: 2 - -two: - id: 2 - revision: 1 - node_id: 4 - -draft1: - id: 100 - revision: 1 - node_id: 2 - -draft2: - id: 101 - revision: 1 - node_id: 3 - -draft3: - id: 102 - revision: 1 - node_id: 4 - -draft4: - id: 103 - revision: 1 - node_id: 5 \ No newline at end of file +# one: +# id: 1 +# revision: 1 +# node_id: 2 +# title: "first" +# +# two: +# id: 2 +# revision: 1 +# node_id: 4 +# title: "second" +# +# draft1: +# id: 100 +# revision: 1 +# node_id: 2 +# title: "third" +# +# draft2: +# id: 101 +# revision: 1 +# node_id: 3 +# title: "fourth" +# +# draft3: +# id: 102 +# revision: 1 +# node_id: 4 +# title: "fifth" +# +# draft4: +# id: 103 +# revision: 1 +# node_id: 5 +# title: "sixth" \ No newline at end of file diff --git a/test/functional/nodes_controller_test.rb b/test/functional/nodes_controller_test.rb index edc49b6..1c7e607 100644 --- a/test/functional/nodes_controller_test.rb +++ b/test/functional/nodes_controller_test.rb @@ -5,6 +5,7 @@ class NodesControllerTest < ActionController::TestCase include AuthenticatedTestHelper def test_get_index + Node.root.descendants.delete_all login_as :quentin get :index assert_response :success @@ -26,6 +27,9 @@ class NodesControllerTest < ActionController::TestCase login_as :quentin node = Node.find_by_unique_name("fourth_child") + node.pages.create + node.draft = node.pages.last + node.save assert_equal 1, node.pages.length diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index e054887..ef298bb 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -5,7 +5,11 @@ class NodeTest < ActiveSupport::TestCase def setup @root = Node.find(1) @first_child = Node.find(2) + @first_child.pages.create! :title => "one" + @first_child.draft = @first_child.pages.last + @first_child.save @second_child = Node.find(3) + @second_child.pages.create! :title => "one" @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' diff --git a/test/unit/page_test.rb b/test/unit/page_test.rb index 8c2fc95..a2083c0 100644 --- a/test/unit/page_test.rb +++ b/test/unit/page_test.rb @@ -22,7 +22,7 @@ class PageTest < ActiveSupport::TestCase d1.tag_list = "update" d1.save n1.publish_draft! - + d2 = n1.find_or_create_draft @user1 n1.publish_draft! @@ -30,7 +30,7 @@ class PageTest < ActiveSupport::TestCase d3.tag_list = "update, pressemitteilung" d3.save n2.publish_draft! - + d4 = n2.find_or_create_draft @user1 n2.publish_draft! @@ -63,7 +63,7 @@ class PageTest < ActiveSupport::TestCase I18n.locale = :de d.body = before - d.save + d.save! assert_equal after, d.body end -- cgit v1.3