cirandas.net

ref: master

plugins/context_content/test/unit/context_content_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require 'test_helper'

class ContextContentBlockTest < ActiveSupport::TestCase

  def setup
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
    @block = ContextContentPlugin::ContextContentBlock.create!
    @block.types = ['TextArticle']
  end

  should 'describe itself' do
    assert_not_equal Block.description, ContextContentPlugin::ContextContentBlock.description
  end

  should 'has a help' do
    assert @block.help
  end

  should 'return nothing if page is nil' do
    assert_equal nil, @block.contents(nil)
  end

  should 'return children of page' do
    folder = fast_create(Folder)
    article = fast_create(TextArticle, :parent_id => folder.id)
    assert_equal [article], @block.contents(folder)
  end

  should 'return parent name of the contents' do
    folder = fast_create(Folder, :name => " New Folder")
    article = fast_create(TextArticle, :parent_id => folder.id)
    assert_equal folder.name, @block.parent_title([article])
  end

  should 'return no parent name if there is no content' do
    assert_nil @block.parent_title([])
  end

  should 'limit number of children to display' do
    @block.limit = 2
    folder = fast_create(Folder)
    article1 = fast_create(TextArticle, :parent_id => folder.id)
    article2 = fast_create(TextArticle, :parent_id => folder.id)
    article3 = fast_create(TextArticle, :parent_id => folder.id)
    assert_equal 2, @block.contents(folder).length
  end

  should 'show contents for next page' do
    @block.limit = 2
    folder = fast_create(Folder)
    article1 = fast_create(TextArticle, :name => 'article 1', :parent_id => folder.id)
    article2 = fast_create(TextArticle, :name => 'article 2', :parent_id => folder.id)
    article3 = fast_create(TextArticle, :name => 'article 3', :parent_id => folder.id)
    assert_equal [article3], @block.contents(folder, 2)
  end

  should 'show parent contents for next page' do
    @block.limit = 2
    folder = fast_create(Folder)
    article1 = fast_create(TextArticle, :name => 'article 1', :parent_id => folder.id)
    article2 = fast_create(TextArticle, :name => 'article 2', :parent_id => folder.id)
    article3 = fast_create(TextArticle, :name => 'article 3', :parent_id => folder.id)
    assert_equal [article3], @block.contents(article1, 2)
  end

  should 'return parent children if page has no children' do
    folder = fast_create(Folder)
    article = fast_create(TextArticle, :parent_id => folder.id)
    assert_equal [article], @block.contents(article)
  end

  should 'do not return parent children if show_parent_content is false' do
    @block.show_parent_content = false
    folder = fast_create(Folder)
    article = fast_create(TextArticle, :parent_id => folder.id)
    assert_equal [], @block.contents(article)
  end

  should 'return nil if a page has no parent' do
    folder = fast_create(Folder)
    assert_equal nil, @block.contents(folder)
  end

  should 'return available content types with checked types first' do
    @block.types = ['TextArticle', 'Folder']
    assert_equal [TextArticle, Folder, UploadedFile, Event, Blog, Forum, Gallery, RssFeed], @block.available_content_types
  end

  should 'return available content types' do
    @block.types = []
    assert_equal [UploadedFile, Event, TextArticle, Folder, Blog, Forum, Gallery, RssFeed], @block.available_content_types
  end

  should 'return first 2 content types' do
    assert_equal 2, @block.first_content_types.length
  end

  should 'return all but first 2 content types' do
    assert_equal @block.available_content_types.length - 2, @block.more_content_types.length
  end

  should 'return 2 as default value for first_types_count' do
    assert_equal 2, @block.first_types_count
  end

  should 'return types length if it has more than 2 selected types' do
    @block.types = ['UploadedFile', 'Event', 'Folder']
    assert_equal 3, @block.first_types_count
  end

  should 'return selected types at first_content_types' do
    @block.types = ['UploadedFile', 'Event', 'Folder']
    assert_equal [UploadedFile, Event, Folder], @block.first_content_types
    assert_equal @block.available_content_types - [UploadedFile, Event, Folder], @block.more_content_types
  end

  should 'include plugin content at available content types' do
    class SomePluginContent;end
    class SomePlugin; def content_types; SomePluginContent end end
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SomePlugin.new])

    @block.types = []
    assert_equal [UploadedFile, Event, TextArticle, Folder, Blog, Forum, Gallery, RssFeed, SomePluginContent], @block.available_content_types
  end

  should 'return box owner on profile method call' do
    profile = fast_create(Community)
    box = Box.create!(:owner => profile)
    block = ContextContentPlugin::ContextContentBlock.create!(:box_id => box.id)
    assert_equal profile, block.profile
  end

  should 'not be cacheable' do
    refute @block.cacheable?
  end

end

require 'boxes_helper'

class ContextContentBlockViewTest < ActionView::TestCase
  include BoxesHelper

  def setup
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
    @block = ContextContentPlugin::ContextContentBlock.create!
    @block.types = ['TextArticle']
  end

  should 'render nothing if it has no content to show' do
    assert_equal "\n", render_block_content(@block)
  end

  should 'render context content block view' do
    @page = fast_create(Folder)
    article = fast_create(TextArticle, :parent_id => @page.id)
    contents = [article]
    @block.use_parent_title = true

    article.expects(:view_url).returns('http://test.noosfero.plugins')
    @block.expects(:contents).with(@page).returns(contents)
    @block.expects(:parent_title).with(contents).returns(@page.name)
    ActionView::Base.any_instance.expects(:block_title).with(@page.name, @block.subtitle).returns("")

    render_block_content(@block)
  end

  should 'do not display pagination links if page is nil' do
    @page = nil

    assert_equal "\n", render_block_content(@block)
  end

  should 'do not display pagination links if it has until one page' do
    assert_equal "\n", render_block_content(@block)
  end

  should 'display pagination links if it has more than one page' do
    @block.limit = 2
    @page = fast_create(Folder)
    article1 = fast_create(TextArticle, :parent_id => @page.id)
    article2 = fast_create(TextArticle, :parent_id => @page.id)
    article3 = fast_create(TextArticle, :parent_id => @page.id)
    contents = [article1, article2, article3]
    contents.each do |article|
      article.expects(:view_url).returns('http://test.noosfero.plugins')
    end

    ActionView::Base.any_instance.expects(:block_title).returns("")
    @block.expects(:contents).with(@page).returns(contents)

    render_block_content(@block)
  end
end