test/unit/version_test.rb in zena-0.16.9 vs test/unit/version_test.rb in zena-1.0.0.beta1
- old
+ new
@@ -1,312 +1,220 @@
require 'test_helper'
class VersionTest < Zena::Unit::TestCase
+ context 'With a logged in visitor' do
+ setup do
+ login(:tiger)
+ end
- def version(sym)
- secure!(Node) { nodes(sym) }.version
- end
+ context 'a version' do
+ subject do
+ versions(:status_en)
+ end
- def test_author
- login(:tiger)
- v = versions(:opening_red_fr)
- assert_equal nodes_id(:tiger), v.author[:id]
- end
+ context 'receiving author' do
+ should 'return a BaseContact' do
+ assert_kind_of BaseContact, subject.author
+ end
- def test_cannot_set_node_id
- login(:tiger)
- node = Node.new(:v_node_id => 1234)
- assert_nil node.version.node_id
- end
+ should 'return the contact node of the author' do
+ assert_equal nodes_id(:ant), subject.author[:id]
+ end
+ end # receiving author
- def test_cannot_set_node_id_with_attributes
- login(:tiger)
- node = secure!(Node) { nodes(:status) }
- original_node_id = node.version.node_id
- node.update_attributes(:v_node_id => nodes_id(:lake) )
- end
+ # Workflow testing....
+ should 'ignore workflow attributes on edited' do
+ subject.attributes = {'title' => 'status title', 'publish_from' => Time.now}
+ assert subject.changed?
+ assert !subject.edited?
+ end
- def test_cannot_set_site_id_with_new_record
- login(:tiger)
- node = Node.new(:site_id => 1234)
- assert_nil node.site_id
- end
+ should 'use properties on edited' do
+ subject.attributes = {'title' => 'Foo title'}
+ assert subject.changed?
+ assert subject.edited?
+ end
- def test_cannot_set_site_id_with_old_record
- login(:tiger)
- node = secure!(Node) { nodes(:status) }
- original_site_id = node.site_id
- node.update_attributes(:v_site_id => 1234)
- assert_equal original_site_id, node.site_id
- end
+ should 'not be marked as edited' do
+ assert !subject.edited?
+ end
- def test_cannot_set_site_id_by_attribute
- login(:tiger)
- node = secure!(Node) { nodes(:status) }
- original_site_id = node.site_id
- node.update_attributes(:v_site_id=>sites_id(:ocean))
- assert_equal original_site_id, node.site_id
- end
+ should 'not be marked as edited on status change' do
+ subject.status = 999
+ assert !subject.edited?
+ end
- def test_cannot_set_node_id_on_create
- login(:tiger)
- node = Node.create(:v_node_id=>nodes_id(:lake))
- assert_nil node.version.node_id
- end
+ should 'be marked as edited on node property change' do
+ subject.node.title = 'new title'
+ assert subject.edited?
+ end
+ end # a version
- def test_cannot_set_content_id
- login(:tiger)
- node = Node.new(:v_content_id => nodes_id(:lake))
- assert_nil node.version.content_id
- end
- def test_cannot_set_content_id_by_attribute
- login(:tiger)
- node = secure!(Node) { nodes(:status) }
- node.update_attributes(:v_content_id=>nodes_id(:lake))
- assert_nil node.version.content_id
- end
+ context 'a redaction' do
+ subject do
+ versions(:opening_red_fr)
+ end
+ end # a redaction
- def test_cannot_set_content_id_on_create
- login(:tiger)
- node = Node.create(:v_content_id=>nodes_id(:lake))
- assert_nil node.version.content_id
- end
+ context 'on node creation' do
+ context 'setting an invalid v_lang' do
+ setup do
+ @node = secure!(Page) { Page.create(:v_lang => 'io', :parent_id => nodes_id(:status), :node_name => 'hello')}
+ end
- def test_new_site_id_set
- login(:ant)
- node = secure!(Node) { Node.create(:v_title=>'super', :parent_id=>nodes_id(:wiki)) }
- assert !node.new_record?, "Not a new record"
- assert_equal sites_id(:zena), node.version.site_id
- end
+ should 'not create record if lang is not allowed' do
+ assert @node.new_record?
+ end
- def test_version_number_edit_by_attribute
- login(:tiger)
- node = secure!(Node) { nodes(:tiger) }
- version = node.version
- assert_equal 1, version.number
- # edit
- node.attributes = {:v_title => 'new title'}
- version = node.version
- assert version.new_record?
- assert_equal 1, version.number # same as original
- # save
- assert node.save, "Node can be saved"
- # version number changed
- version = node.version
- assert_equal 2, version.number
- end
+ should 'return an error on v_lang' do
+ assert @node.errors[:v_lang].any?
+ end
+ end
- def test_version_number_edit
- login(:tiger)
- node = secure!(Node) { nodes(:tiger) }
- version = node.version
- assert_equal 1, version.number
- # can edit
- assert node.update_attributes(:v_title=>'new title')
- # saved
- # version number changed
- version = node.version
- assert_equal 2, version.number
- end
+ context 'setting a valid v_lang' do
+ subject do
+ @node = secure!(Page) { Page.create(:v_lang => 'de', :parent_id => nodes_id(:status), :node_name => 'hello')}
+ end
- def test_presence_of_node
- login(:tiger)
- node = secure!(Node) { Node.new(:parent_id=>nodes_id(:zena), :name=>'bob') }
- assert node.save
- vers = Version.new
- assert !vers.save
- assert_equal "can't be blank", vers.errors[:node]
- end
+ should 'create a single version' do
+ assert_difference('Version.count', 1) do
+ subject
+ end
+ end
- def test_update_content_one_version
- preserving_files("test.host/data") do
- login(:ant)
- visitor.lang = 'en'
- node = secure!(Node) { nodes(:forest_pdf) }
- assert_equal Zena::Status[:red], node.version.status
- assert_equal versions_id(:forest_pdf_en), node.version.content.version_id
- assert_equal 63569, node.version.content.size
- # single redaction in redit time
- node.version.created_at = Time.now
- assert node.update_attributes(:c_file=>uploaded_pdf('water.pdf')), 'Can edit node'
- # version and content object are the same
- assert_equal versions_id(:forest_pdf_en), node.version.content.version_id
- # content changed
- assert_equal 29279, node.version.content.size
- assert_kind_of File, node.version.content.file
- assert_equal 29279, node.version.content.file.stat.size
- end
- end
+ should 'set version lang' do
+ assert_equal 'de', subject.version.lang
+ end
+ end
+ end # setting v_lang
- def test_remap_master_version_if_many_use_same_content
- preserving_files("test.host/data") do
- login(:ant)
- visitor.lang = 'fr'
- node = secure!(Node) { nodes(:forest_pdf) }
- old_vers_id = node.version.id
- # ant's english redaction
- assert_equal 'en', node.version.lang
- content_id_before_move = node.version.content.id
+ context 'updating a version' do
+ setup do
+ @node = secure!(Node) { nodes(:status) }
+ end
- # 1. Create a new version in french
- assert node.update_attributes(:v_title=>'les arbres')
+ subject do
+ @node.version
+ end
- assert node.propose # only proposed/published versions block
- assert_equal 'fr', node.version.lang
+ should 'not allow setting node_id' do
+ subject.update_attributes(:node_id => nodes_id(:lake))
+ assert_equal nodes_id(:status), subject.node_id
+ end
- # new redaction for french
- assert_not_equal node.version.id, old_vers_id
+ should 'not allow setting attachment_id' do
+ subject.update_attributes(:attachment_id => attachments_id(:bird_jpg_en))
+ assert_nil subject.attachment_id
+ end
- # 2. New redaction points to old content
- assert_equal node.version.content_id, old_vers_id
+ should 'not allow setting site_id' do
+ subject.update_attributes(:site_id => sites_id(:ocean))
+ assert_equal sites_id(:zena), subject.site_id
+ end
- login(:ant)
- visitor.lang = 'en'
- node = secure!(Node) { nodes(:forest_pdf) }
+ should 'not allow setting number' do
+ subject.update_attributes(:number => 5)
+ assert_equal 1, subject.number
+ end
- # 3. Edit ant's original (english) redaction
- assert_equal old_vers_id, node.version.id
+ context 'with a locale and time_zone' do
+ setup do
+ I18n.locale = 'fr'
+ visitor.time_zone = 'Asia/Jakarta'
+ end
- # edit content (should move content's master_version to other version and create a new content)
- node.version.created_at = Time.now # force redit time
- assert node.update_attributes(:c_file=>uploaded_pdf('water.pdf'))
- assert_nil node.version.content_id # we have our own content
- assert_equal node.version.id, node.version.content.version_id
- # this is still the original (english) version
- assert_equal old_vers_id, node.version.id
+ should 'parse publish_from date depending on time_zone' do
+ subject.update_attributes('publish_from' => '9-9-2009 15:17')
+ assert_equal Time.utc(2009,9,9,8,17), subject.publish_from
+ end
+ end
+ end # updating a version
- login(:ant)
- visitor.lang = 'fr'
- node = secure!(Node) { nodes(:forest_pdf) }
+ context 'updating a node' do
+ subject do
+ secure!(Node) { nodes(:ant) }
+ end
- # 4. The content has become our own
- assert_equal content_id_before_move, node.version.content.id
- assert_nil node.version.content_id
- assert_equal node.version.id, node.version.content.version_id
- end
- end
+ should 'increase version number' do
+ assert_difference('subject.version.number', 1) do
+ subject.update_attributes(:first_name => 'John')
+ end
+ end
- def test_dynamic_attributes
- login(:tiger)
- node = secure!(Node) { nodes(:status) }
- version = node.send(:redaction)
- assert_nothing_raised { version.dyn['zucchini'] = 'courgettes' }
- assert_nothing_raised { version.dyn_attributes = {'zucchini' => 'courgettes' }}
- assert_equal 'courgettes', version.dyn['zucchini']
- assert node.save
+ should 'create a new version' do
+ assert_difference('Version.count', 1) do
+ subject.update_attributes(:first_name => 'Foobar')
+ end
+ end
- node = secure!(Node) { nodes(:status) }
- version = node.version
- assert_equal 'courgettes', version.dyn['zucchini']
- end
+ context 'in redit time' do
+ setup do
+ subject.update_attributes(:first_name => 'Annette')
+ end
- def test_clone
- login(:tiger)
- node = secure!(Node) { nodes(:status) }
- assert node.update_attributes(:d_whatever => 'no idea')
- assert_equal 'no idea', node.version.dyn['whatever']
- version1_id = node.version[:id]
- assert node.publish
- version1_publish_from = node.version.publish_from
+ context 'in the same v_lang' do
+ should 'not create a new version' do
+ assert_difference('Version.count', 0) do
+ subject.update_attributes(:first_name => 'Bug')
+ end
+ end
- node = secure!(Node) { nodes(:status) }
- assert node.update_attributes(:d_other => 'funny')
- version2_id = node.version[:id]
- assert_not_equal version1_id, version2_id
- assert_equal 'no idea', node.version.dyn['whatever']
- assert_equal 'funny', node.version.dyn['other']
- assert_equal version1_publish_from, node.version.publish_from
- end
+ should 'create a new version if backup required' do
+ assert_difference('Version.count', 1) do
+ assert subject.update_attributes('first_name'=>'Eric', 'v_backup' => 'true')
+ end
+ end
+ end
- def test_would_edit
- v = versions(:zena_en)
- assert v.would_edit?('title' => 'different')
- assert v.would_edit?('dyn_attributes' => {'foo' => 'different'})
- assert !v.would_edit?('title' => v.title)
- assert !v.would_edit?('status' => Zena::Status[:red])
- assert !v.would_edit?('illegal_field' => 'whatever')
- assert !v.would_edit?('node_id' => 'whatever')
- end
+ context 'with a new v_lang' do
+ should 'create a new redaction' do
+ assert_difference('subject.versions.count', 1) do
+ assert_difference('Version.count', 1) do
+ assert subject.update_attributes(:v_lang => 'de')
+ end
+ assert_equal 'de', subject.v_lang
+ end
+ end
+ end
+ end # in redit time
+ end # updating a node
+ end # With a logged in visitor
- def test_would_edit_content
- v = versions(:ant_en)
- assert v.would_edit?('content_attributes' => {'name' => 'different'})
- assert !v.would_edit?('content_attributes' => {'name' => v.content.name})
- end
+ context 'A new version' do
+ should 'not allow setting node_id' do
+ version = Version.new(:node_id => 1234)
+ assert_nil version.node_id
+ end
- def test_new_version_is_edited
- v = Version.new
- assert v.edited?
- v.title = 'hooo'
- assert v.edited?
- end
+ should 'not allow setting attachment_id' do
+ version = Version.new(:attachment_id => attachments_id(:bird_jpg_en))
+ assert_nil version.attachment_id
+ end
- def test_edited
- v = versions(:zena_en)
- assert !v.edited?
- v.status = 999
- assert !v.edited?
- v.title = 'new title'
- assert v.edited?
- end
+ should 'not allow setting site_id' do
+ version = Version.new(:site_id => sites_id(:ocean))
+ assert_nil version.site_id
+ end
- def test_edited_changed_content
- v = versions(:ant_en)
- v.content_attributes = {'name' => 'Invicta'}
- assert !v.edited?
- v.content_attributes = {'name' => 'New name'}
- assert v.edited?
- end
+ should 'not allow setting number' do
+ version = Version.new(:number => 5)
+ assert_equal 1, version.number
+ end
- def test_bad_lang
- login(:tiger)
- node = secure!(Page) { Page.create(:v_lang => 'io', :parent_id => nodes_id(:status), :name => 'hello', :v_title => '')}
- assert node.new_record?
- assert node.errors[:version_lang].any?
- end
-
- def test_set_v_lang
- login(:tiger)
- assert_equal 'en', visitor.lang
- node = secure!(Page) { Page.create(:v_lang => 'fr', :parent_id => nodes_id(:status), :name => 'hello', :v_title => '')}
- assert !node.new_record?
- assert_equal 'fr', node.version.lang
- end
-
- def test_create_version_other_lang
- login(:tiger)
- assert_equal 'en', visitor.lang
- node = secure!(Node) { nodes(:projects) }
- en_version = node.version
- assert node.update_attributes(:v_lang => 'fr', :v_title => 'projets')
- assert !node.new_record?
- assert_equal 'fr', node.version.lang
- assert_not_equal en_version.id, node.version.id
- end
-
- def test_should_parse_publish_from_date
- I18n.locale = 'fr'
- visitor.time_zone = 'Asia/Jakarta'
- v = Version.new('publish_from' => '9-9-2009 15:17')
- assert_equal Time.utc(2009,9,9,8,17), v.publish_from
- end
-
- context 'A visitor with write access on a redaction with dyn attributes' do
- setup do
- login(:tiger)
- node = secure(Node) { nodes(:nature) }
- node.update_attributes(:d_foo => 'bar')
- @node = secure(Node) { nodes(:nature) } # reload
+ should 'set site_id on save' do
+ version = Version.new(:status => Zena::Status[:red])
+ version.node = secure!(Node) { nodes(:status) }
+ assert version.save
+ assert_equal sites_id(:zena), version.site_id
end
- should 'see dyn attribute' do
- assert_equal 'bar', @node.version.dyn['foo']
+ should 'not save if node is not set' do
+ version = Version.new(:status => Zena::Status[:red])
+ assert !version.save
+ assert_equal "can't be blank", version.errors[:node]
end
- should 'see be able to update dyn attribute' do
- assert @node.version.dyn.would_edit?('foo' => 'max')
- assert @node.update_attributes(:d_foo => 'max')
- @node = secure(Node) { nodes(:nature) }
- assert_equal 'max', @node.version.dyn['foo']
+ should 'mark as edited' do
+ assert subject.edited?
end
- end
+ end # A new version
end