cirandas.net

ref: master

plugins/display_content/test/functional/display_content_plugin_myprofile_controller_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
require_relative '../test_helper'
require_relative '../../controllers/display_content_plugin_myprofile_controller'

class DisplayContentPluginMyprofileControllerTest < ActionController::TestCase

  def setup
    @controller = DisplayContentPluginMyprofileController.new

    user = create_user('testinguser')
    login_as(user.login)
    @profile = user.person
    @environment = @profile.environment

    @environment.enabled_plugins = ['DisplaContentPlugin']
    @environment.save!

#    box = Box.new(:owner => @environment, :position => 1)
#    box.save

    DisplayContentBlock.delete_all
    @block = DisplayContentBlock.new
    @block.box = @profile.boxes.first
    @block.save!
  end

  attr_accessor :profile, :block

  should 'access index action' do
    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    assert_response :success
  end

  should 'index action returns a empty json if there is no content' do
    Article.delete_all
    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    assert_equal [], json_response
  end

  should 'index action returns an json with node content' do
    Article.delete_all
    article = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)

    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    expected_json = {'data' => article.title}
    expected_json['attr'] = { 'node_id' => article.id, 'parent_id' => article.parent_id}

    assert_hash_equivalent [expected_json], json_response
  end

  should 'index action returns an json with node checked if the node is in the nodes list' do
    Article.delete_all
    article = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    block.nodes= [article.id]
    block.save!

    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    expected_json = {'data' => article.title}
    expected_json['attr'] = { 'node_id' => article.id, 'parent_id' => article.parent_id}
    expected_json['attr'].merge!({'class' => 'jstree-checked'})

    assert_hash_equivalent [expected_json], json_response
  end

  should 'index action returns an json with node undetermined if the node is in the parent nodes list' do
    Article.delete_all
    f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    article = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id)
    article2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id)
    block.nodes = [article.id]
    block.save!

    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    expected_json = { 'node_id' => f.id, 'class' => 'jstree-undetermined', 'parent_id' => f.parent_id}
    assert_equal expected_json, json_response.first['attr']
  end

  should 'index action returns an json with node closed if the node has article with children' do
    Article.delete_all
    f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    article = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id)
    block.save!

    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    expected_json = {'data' => f.title}
    expected_json['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id}
    expected_json['state'] = 'closed'

    assert_hash_equivalent [expected_json], json_response
  end

  should 'index action returns an json with all the children nodes if some parent is in the parents list' do
    Article.delete_all
    f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id)
    block.checked_nodes = {a1.id => true}
    block.save!

    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    expected_json = {'data' => f.title}
    expected_json['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id}
    children = [
      {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id, "class" => "jstree-checked"}},
      {'data' => a2.title, 'attr' => {'node_id' => a2.id, 'parent_id'=> a2.parent_id}}
    ]
    expected_json['attr'].merge!({'class' => 'jstree-undetermined'})
    expected_json['children'] = children
    expected_json['state'] = 'closed'

    assert_hash_equivalent [expected_json], json_response
  end

  should 'index action returns an json with all the children nodes and root nodes if some parent is in the parents list and there is others root articles' do
    Article.delete_all
    f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id)
    block.checked_nodes = {a1.id => true}
    block.save!

    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    expected_json = []
    value = {'data' => f.title}
    value['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id}
    children = [
      {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id, "class" => "jstree-checked"}},
      {'data' => a2.title, 'attr' => {'node_id' => a2.id, 'parent_id'=> a2.parent_id}}
    ]
    value['attr'].merge!({'class' => 'jstree-undetermined'})
    value['children'] = children
    value['state'] = 'closed'
    expected_json.push(value)

    value = {'data' => a3.title}
    value['attr'] = { 'node_id' => a3.id, 'parent_id' => a3.parent_id}
    expected_json.push(value)

    assert_hash_equivalent expected_json, json_response
  end

  should 'index action returns an json without children nodes if the parent is not in the parents list' do
    Article.delete_all
    f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id)

    get :index, :block_id => block.id, :profile => profile.identifier
    json_response = ActiveSupport::JSON.decode(@response.body)
    expected_json = []
    value = {'data' => f.title}
    value['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id}
    value['state'] = 'closed'
    expected_json.push(value)

    value = {'data' => a3.title}
    value['attr'] = { 'node_id' => a3.id, 'parent_id' => a3.parent_id}
    expected_json.push(value)

    assert_hash_equivalent expected_json, json_response
  end

end