cirandas.net

ref: master

test/unit/article_block_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require_relative "../test_helper"

class ArticleBlockTest < ActiveSupport::TestCase
  include ApplicationHelper

  should 'describe itself' do
    assert_not_equal Block.description, ArticleBlock.description
  end

  should 'refer to an article' do
    profile = create_user('testuser').person
    article = profile.articles.build(:name => 'test article')
    article.save!

    block = ArticleBlock.new
    block.article = article

    block.save!

    assert_equal article, Block.find(block.id).article

  end

  should 'not crash when referenced article is removed' do
    person = create_user('testuser').person
    a = person.articles.create!(:name => 'test')
    block = ArticleBlock.create.tap do |b|
      b.article = a
    end
    person.boxes.first.blocks << block
    block.save!

    a.destroy
    block.reload
    assert_nil block.article
  end

  should 'nullify reference to unexisting article' do
    Article.delete_all

    block = ArticleBlock.new
    block.article_id = 999

    block.article
    assert_nil block.article_id
  end

  should "take available articles with a person as the box owner" do
    person = create_user('testuser').person
    person.articles.delete_all
    assert_equal [], person.articles
    a = person.articles.create!(:name => 'test')
    block = ArticleBlock.create.tap do |b|
      b.article = a
    end
    person.boxes.first.blocks << block
    block.save!

    block.reload
    assert_equal [a],block.available_articles
  end

  should "take available articles with an environment as the box owner" do
    env = Environment.create!(:name => 'test env')
    env.profiles.each { |profile| profile.articles.destroy_all }
    assert_equal [], env.articles
    community = fast_create(Community)
    a = fast_create(TextArticle, :profile_id => community.id, :name => 'test')
    env.portal_community=community
    env.save
    block = create(ArticleBlock, :article => a)
    env.boxes.first.blocks << block
    block.save!

    block.reload
    assert_equal [a],block.available_articles
  end

  protected
    include NoosferoTestHelper

end

require 'boxes_helper'
require 'block_helper'

class ArticleBlockViewTest < ActionView::TestCase
  include BoxesHelper

  ActionView::Base.send :include, ArticleHelper
  ActionView::Base.send :include, ButtonsHelper
  ActionView::Base.send :include, BlockHelper

  should "take article's content" do
    block = ArticleBlock.new
    article = mock
    article.expects(:to_html).returns("Article content")
    block.stubs(:article).returns(article)
    ActionView::Base.any_instance.stubs(:block_title).returns("")

    assert_match(/Article content/, render_block_content(block))
  end

  should "display empty title if title is blank" do
    block = ArticleBlock.new
    article = mock
    article.expects(:to_html).returns("Article content")
    block.expects(:title).returns('')
    block.stubs(:article).returns(article)

    assert_tag_in_string render_block_content(block),
         :tag => 'h3', :attributes => {:class => 'block-title empty'},
         :descendant => { :tag => 'span' }
  end

  should "display title if defined" do
    block = ArticleBlock.new
    article = mock
    article.expects(:to_html).returns("Article content")
    block.expects(:title).returns('Article title')
    block.stubs(:article).returns(article)

    assert_tag_in_string render_block_content(block),
          :tag => 'h3', :attributes => {:class => 'block-title'},
          :descendant => { :tag => 'span', :content => 'Article title' }
  end

  should 'display image if article is an image' do
    profile = create_user('testuser').person
    block = ArticleBlock.new
    image = create(UploadedFile, :profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))

    block.article = image
    block.save!

    assert_tag_in_string render_block_content(block),
        :tag => 'img',
        :attributes => {
            :src => image.public_filename(:display),
            :class => /file-image/
        }
  end

  should 'not display gallery pages navigation in content' do
    profile = create_user('testuser').person
    block = ArticleBlock.new
    gallery = fast_create(Gallery, :profile_id => profile.id)
    image = create(UploadedFile, :profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => gallery)
    block.article = image
    block.save!

    assert_no_match(/Previous/, render_block_content(block))
  end

  should 'display link to archive if article is an archive' do
    profile = create_user('testuser').person
    block = ArticleBlock.new
    file = create(UploadedFile, :profile => profile, :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'))

    block.article = file
    block.save!

    UploadedFile.any_instance.stubs(:url).returns('myhost.mydomain/path/to/file')
    assert_tag_in_string render_block_content(block), :tag => 'a', :content => _('Download')
  end
end