2011-12-22 23:45:32 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Locomotive::ContentEntry do
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
Locomotive::Site.any_instance.stubs(:create_default_pages!).returns(true)
|
|
|
|
@content_type = FactoryGirl.build(:content_type)
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_type.entries_custom_fields.build :label => 'Title', :type => 'string'
|
|
|
|
@content_type.entries_custom_fields.build :label => 'Description', :type => 'text'
|
|
|
|
@content_type.entries_custom_fields.build :label => 'Visible ?', :type => 'boolean', :name => 'visible'
|
2012-03-05 22:17:33 +00:00
|
|
|
@content_type.entries_custom_fields.build :label => 'File', :type => 'file'
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_type.valid?
|
2012-03-12 02:09:20 +00:00
|
|
|
@content_type.send(:set_label_field)
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#validation' do
|
2012-02-16 11:42:41 +00:00
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
it 'is valid' do
|
|
|
|
build_content_entry.should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
## Validations ##
|
|
|
|
|
|
|
|
it 'requires the presence of title' do
|
2012-02-16 11:42:41 +00:00
|
|
|
content_entry = build_content_entry :title => nil
|
2011-12-22 23:45:32 +00:00
|
|
|
content_entry.should_not be_valid
|
|
|
|
content_entry.errors[:title].should == ["can't be blank"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'requires the presence of the permalink (_slug)' do
|
2012-02-16 11:42:41 +00:00
|
|
|
content_entry = build_content_entry :title => nil
|
2011-12-22 23:45:32 +00:00
|
|
|
content_entry.should_not be_valid
|
|
|
|
content_entry.errors[:_slug].should == ["can't be blank"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'setting the slug' do
|
|
|
|
before :each do
|
|
|
|
build_content_entry(:_slug => 'dogs').tap(&:save!)._slug.should == 'dogs'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the given slug if it is unique' do
|
|
|
|
build_content_entry(:_slug => 'monkeys').tap(&:save!)._slug.should == 'monkeys'
|
|
|
|
build_content_entry(:_slug => 'cats-2').tap(&:save!)._slug.should == 'cats-2'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends a number to the end of the slug if it is not unique' do
|
|
|
|
build_content_entry(:_slug => 'dogs').tap(&:save!)._slug.should == 'dogs-1'
|
|
|
|
build_content_entry(:_slug => 'dogs').tap(&:save!)._slug.should == 'dogs-2'
|
|
|
|
build_content_entry(:_slug => 'dogs-2').tap(&:save!)._slug.should == 'dogs-3'
|
|
|
|
build_content_entry(:_slug => 'dogs-2').tap(&:save!)._slug.should == 'dogs-4'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores the case of a slug' do
|
|
|
|
build_content_entry(:_slug => 'dogs').tap(&:save!)._slug.should == 'dogs-1'
|
|
|
|
build_content_entry(:_slug => 'DOGS').tap(&:save!)._slug.should == 'dogs-2'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly handles slugs with multiple numbers' do
|
|
|
|
build_content_entry(:_slug => 'fish-1-2').tap(&:save!)._slug.should == 'fish-1-2'
|
|
|
|
build_content_entry(:_slug => 'fish-1-2').tap(&:save!)._slug.should == 'fish-1-3'
|
|
|
|
|
|
|
|
build_content_entry(:_slug => 'fish-1-hi').tap(&:save!)._slug.should == 'fish-1-hi'
|
|
|
|
build_content_entry(:_slug => 'fish-1-hi').tap(&:save!)._slug.should == 'fish-1-hi-1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-14 00:16:01 +00:00
|
|
|
context '#i18n' do
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
localize_content_type @content_type
|
|
|
|
::Mongoid::Fields::I18n.locale = 'en'
|
|
|
|
@content_entry = build_content_entry(:title => 'Hello world')
|
|
|
|
::Mongoid::Fields::I18n.locale = 'fr'
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
::Mongoid::Fields::I18n.locale = 'en'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tells if an entry has been translated or not' do
|
|
|
|
@content_entry.translated?.should be_false
|
|
|
|
@content_entry.title = 'Bonjour'
|
|
|
|
@content_entry.translated?.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
describe "#navigation" do
|
|
|
|
before(:each) do
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_type.save
|
|
|
|
|
|
|
|
%w(first second third).each_with_index do |item, index|
|
|
|
|
content = build_content_entry(:title => item.to_s, :_position => index)
|
|
|
|
content.save
|
2011-12-22 23:45:32 +00:00
|
|
|
instance_variable_set "@#{item}", content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find previous item when available' do
|
2012-02-16 11:42:41 +00:00
|
|
|
@second.previous.title.should == 'first'
|
|
|
|
@second.previous._position.should == 1
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find next item when available' do
|
2012-02-16 11:42:41 +00:00
|
|
|
@second.next.title.should == 'third'
|
|
|
|
@second.next._position.should == 3
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return nil when fetching previous item on first in list' do
|
|
|
|
@first.previous.should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return nil when fetching next item on last in list' do
|
|
|
|
@third.next.should == nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#permalink' do
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@content_entry = build_content_entry
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a default value based on the highlighted field' do
|
|
|
|
@content_entry.send(:set_slug)
|
|
|
|
@content_entry._permalink.should == 'locomotive'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is empty if no value for the highlighted field is provided' do
|
|
|
|
@content_entry.title = nil; @content_entry.send(:set_slug)
|
|
|
|
@content_entry._permalink.should be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes dashes instead of white spaces' do
|
|
|
|
@content_entry.title = 'my content instance'; @content_entry.send(:set_slug)
|
|
|
|
@content_entry._permalink.should == 'my-content-instance'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes accentued characters' do
|
|
|
|
@content_entry.title = "une chèvre dans le pré"; @content_entry.send(:set_slug)
|
|
|
|
@content_entry._permalink.should == 'une-chevre-dans-le-pre'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes dots' do
|
|
|
|
@content_entry.title = "my.test"; @content_entry.send(:set_slug)
|
2012-04-08 06:42:19 +00:00
|
|
|
@content_entry._permalink.should == 'my-test'
|
2012-04-07 17:25:36 +00:00
|
|
|
end
|
2012-04-08 07:13:07 +00:00
|
|
|
|
2012-04-07 17:25:36 +00:00
|
|
|
it 'accepts non-latin chars' do
|
|
|
|
@content_entry.title = "абракадабра"; @content_entry.send(:set_slug)
|
|
|
|
@content_entry._permalink.should == 'abrakadabra'
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
2012-03-05 22:17:33 +00:00
|
|
|
it 'also accepts a file field as the highlighted field' do
|
|
|
|
@content_entry.stubs(:_label_field_name).returns('file')
|
|
|
|
@content_entry.file = FixturedAsset.open('5k.png'); @content_entry.send(:set_slug)
|
|
|
|
@content_entry._permalink.should == '5k'
|
|
|
|
end
|
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#visibility' do
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@content_entry = build_content_entry
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is visible by default' do
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_entry.send(:set_visibility)
|
|
|
|
@content_entry.visible?.should be_true
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be visible even if it is nil' do
|
|
|
|
@content_entry.visible = nil
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_entry.send(:set_visibility)
|
|
|
|
@content_entry.visible?.should be_true
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can not be visible' do
|
|
|
|
@content_entry.visible = false
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_entry.send(:set_visibility)
|
|
|
|
@content_entry.visible?.should be_false
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#requirements' do
|
|
|
|
|
|
|
|
it 'has public access to the highlighted field value' do
|
2012-02-16 11:42:41 +00:00
|
|
|
build_content_entry._label.should == 'Locomotive'
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-02-16 11:42:41 +00:00
|
|
|
describe '#public_submission' do
|
2011-12-22 23:45:32 +00:00
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@account_1 = FactoryGirl.build('admin user', :id => fake_bson_id('1'))
|
|
|
|
@account_2 = FactoryGirl.build('frenchy user', :id => fake_bson_id('2'))
|
|
|
|
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_type.public_submission_enabled = true
|
2012-04-13 12:38:00 +00:00
|
|
|
@content_type.public_submission_accounts = ['', @account_1._id, @account_2._id.to_s]
|
2011-12-22 23:45:32 +00:00
|
|
|
|
2012-02-16 11:42:41 +00:00
|
|
|
site = FactoryGirl.build(:site)
|
|
|
|
site.stubs(:accounts).returns([@account_1, @account_2])
|
2011-12-22 23:45:32 +00:00
|
|
|
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_entry = build_content_entry(:site => site)
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send email notifications if the api is disabled' do
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_type.public_submission_enabled = false
|
2011-12-22 23:45:32 +00:00
|
|
|
Locomotive::Notifications.expects(:new_content_entry).never
|
|
|
|
@content_entry.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send email notifications if no api accounts' do
|
2012-02-16 11:42:41 +00:00
|
|
|
@content_type.public_submission_accounts = nil
|
2011-12-22 23:45:32 +00:00
|
|
|
Locomotive::Notifications.expects(:new_content_entry).never
|
|
|
|
@content_entry.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends email notifications when a new instance is created' do
|
2012-02-16 11:42:41 +00:00
|
|
|
Locomotive::Notifications.expects(:new_content_entry).with(@account_1, @content_entry).returns(mock('mailer', :deliver => true))
|
|
|
|
Locomotive::Notifications.expects(:new_content_entry).with(@account_2, @content_entry).returns(mock('mailer', :deliver => true))
|
2011-12-22 23:45:32 +00:00
|
|
|
@content_entry.save
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#site' do
|
2012-02-16 11:42:41 +00:00
|
|
|
|
|
|
|
it 'assigns a site when saving the content entry' do
|
|
|
|
content_entry = build_content_entry
|
|
|
|
content_entry.save
|
|
|
|
content_entry.site.should_not be_nil
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
2012-02-16 11:42:41 +00:00
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
2012-03-14 00:16:01 +00:00
|
|
|
def localize_content_type(content_type)
|
|
|
|
content_type.entries_custom_fields.first.localized = true
|
|
|
|
content_type.save
|
|
|
|
end
|
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
def build_content_entry(options = {})
|
2012-03-14 00:16:01 +00:00
|
|
|
@content_type.entries.build({ :title => 'Locomotive', :description => 'Lorem ipsum....', :_label_field_name => 'title' }.merge(options))
|
2011-12-22 23:45:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def fake_bson_id(id)
|
|
|
|
BSON::ObjectId(id.to_s.rjust(24, '0'))
|
|
|
|
end
|
|
|
|
end
|