hydra-collections 3.0.0.beta2 → 3.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,21 +14,21 @@ class OtherCollection < ActiveFedora::Base
14
14
  include Hydra::Collection
15
15
  include Hydra::Collections::Collectible
16
16
 
17
- def to_solr(solr_doc={})
18
- super.tap do |solr_doc|
19
- index_collection_ids(solr_doc)
20
- end
17
+ def to_solr(solr_doc={}, opts={})
18
+ super(solr_doc, opts)
19
+ solr_doc = index_collection_pids(solr_doc)
20
+ return solr_doc
21
21
  end
22
- end
23
22
 
23
+ end
24
24
  class Member < ActiveFedora::Base
25
25
  include Hydra::Collections::Collectible
26
26
  attr_accessor :title
27
27
 
28
- def to_solr(solr_doc={})
29
- super.tap do |solr_doc|
30
- index_collection_ids(solr_doc)
31
- end
28
+ def to_solr(solr_doc={}, opts={})
29
+ super(solr_doc, opts)
30
+ solr_doc = index_collection_pids(solr_doc)
31
+ return solr_doc
32
32
  end
33
33
 
34
34
  end
@@ -75,10 +75,12 @@ describe OtherCollectionsController, :type => :controller do
75
75
  end
76
76
  it "should show the collections" do
77
77
  routes.draw { resources :other_collections, except: :index }
78
- get :show, id: collection
79
- expect(assigns[:collection].title).to eq collection.title
80
- ids = assigns[:member_docs].map(&:id)
81
- expect(ids).to include(asset1.id, asset2.id, asset3.id)
78
+ get :show, id: collection.id
79
+ expect(assigns[:collection].title).to eq(collection.title)
80
+ ids = assigns[:member_docs].map {|d| d.id}
81
+ expect(ids).to include asset1.pid
82
+ expect(ids).to include asset2.pid
83
+ expect(ids).to include asset3.pid
82
84
  end
83
85
  end
84
86
 
@@ -24,7 +24,7 @@ describe SelectsCollectionsController, :type => :controller do
24
24
  end
25
25
 
26
26
  describe "Select Collections" do
27
- before do
27
+ before (:all) do
28
28
  @user = FactoryGirl.find_or_create(:user)
29
29
  @collection = Collection.new title:"Test Public"
30
30
  @collection.apply_depositor_metadata(@user.user_key)
@@ -41,34 +41,36 @@ describe SelectsCollectionsController, :type => :controller do
41
41
  @collection4 = Collection.new title:"Test No Access"
42
42
  @collection4.apply_depositor_metadata('abc123@test.com')
43
43
  @collection4.save
44
+ @collections = []
45
+ (1..11).each do |i|
46
+ collection = Collection.new title:"Test Public #{i}"
47
+ collection.apply_depositor_metadata(@user.user_key)
48
+ collection.read_groups = ["public"]
49
+ collection.save
50
+ @collections << collection
51
+ end
52
+ end
53
+ after (:all) do
54
+ Collection.delete_all
44
55
  end
45
-
46
56
  describe "Public Access" do
47
- let(:user_collections) do
57
+ before (:each) do
48
58
  subject.find_collections
49
- subject.instance_variable_get(:@user_collections)
59
+ @user_collections = subject.instance_variable_get (:@user_collections)
60
+ expect(@user_collections).to be_kind_of(Array)
50
61
  end
51
-
52
- it "should only return public collections" do
53
- expect(user_collections.map(&:id)).to match_array [@collection.id]
62
+ it "should return public collections" do
63
+ expect(@user_collections.index{|d| d.id == @collection.id}).not_to be_nil
54
64
  end
55
-
56
- context "when there are more than 10" do
57
- before do
58
- 11.times do |i|
59
- Collection.new(title:"Test Public #{i}").tap do |col|
60
- col.apply_depositor_metadata(@user.user_key)
61
- col.read_groups = ["public"]
62
- col.save!
63
- end
64
- end
65
- end
66
- it "should return all public collections" do
67
- expect(user_collections.count).to eq(12)
68
- end
65
+ it "should return all public collections" do
66
+ expect(@user_collections.count).to eq(12)
69
67
  end
68
+ it "should not return non public collections" do
69
+ expect(@user_collections.index{|d| d.id == @collection2.id}).to be_nil
70
+ expect(@user_collections.index{|d| d.id == @collection3.id}).to be_nil
71
+ expect(@user_collections.index{|d| d.id == @collection4.id}).to be_nil
72
+ end
70
73
  end
71
-
72
74
  describe "Read Access" do
73
75
  describe "not signed in" do
74
76
  it "should error if the user is not signed in" do
@@ -76,38 +78,45 @@ describe SelectsCollectionsController, :type => :controller do
76
78
  end
77
79
  end
78
80
  describe "signed in" do
79
- before { sign_in @user }
80
-
81
- let(:user_collections) do
81
+ before (:each) do
82
+ sign_in @user
82
83
  subject.find_collections_with_read_access
83
- subject.instance_variable_get(:@user_collections)
84
+ @user_collections = subject.instance_variable_get (:@user_collections)
85
+ expect(@user_collections).to be_kind_of(Array)
84
86
  end
85
-
86
- it "should return only public and read access (edit access implies read) collections" do
87
- expect(user_collections.map(&:id)).to match_array [@collection.id, @collection2.id, @collection3.id]
87
+ it "should return public and read access (edit access implies read) collections" do
88
+ expect(@user_collections.index{|d| d.id == @collection.id}).not_to be_nil
89
+ expect(@user_collections.index{|d| d.id == @collection2.id}).not_to be_nil
90
+ expect(@user_collections.index{|d| d.id == @collection3.id}).not_to be_nil
88
91
  end
92
+ it "should not return non public collections" do
93
+ expect(@user_collections.index{|d| d.id == @collection4.id}).to be_nil
94
+ end
89
95
  end
90
96
  end
91
-
92
97
  describe "Edit Access" do
93
98
  describe "not signed in" do
94
99
  it "should error if the user is not signed in" do
95
100
  expect { subject.find_collections_with_edit_access }.to raise_error
96
101
  end
97
102
  end
98
-
99
103
  describe "signed in" do
100
- before { sign_in @user }
101
-
102
- let(:user_collections) do
104
+ before (:each) do
105
+ sign_in @user
103
106
  subject.find_collections_with_edit_access
104
- subject.instance_variable_get(:@user_collections)
105
- end
106
-
107
- it "should return only public or editable collections" do
108
- expect(user_collections.map(&:id)).to match_array [@collection.id, @collection3.id]
107
+ @user_collections = subject.instance_variable_get (:@user_collections)
108
+ expect(@user_collections).to be_kind_of(Array)
109
+ end
110
+ it "should return public or editable collections" do
111
+ expect(@user_collections.index{|d| d.id == @collection.id}).not_to be_nil
112
+ expect(@user_collections.index{|d| d.id == @collection3.id}).not_to be_nil
109
113
  end
114
+ it "should not return non public or editable collections" do
115
+ expect(@user_collections.index{|d| d.id == @collection2.id}).to be_nil
116
+ expect(@user_collections.index{|d| d.id == @collection4.id}).to be_nil
117
+ end
110
118
  end
111
119
  end
112
120
  end
121
+
113
122
  end
data/spec/factories.rb CHANGED
@@ -1,3 +1,17 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
1
15
  FactoryGirl.define do
2
16
  factory :collection
3
17
  end
File without changes
@@ -1,3 +1,17 @@
1
+ # Copyright © 2012 The Pennsylvania State University
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
1
15
  FactoryGirl.define do
2
16
  factory :user, :class => User do |u|
3
17
  email 'jilluser@example.com'
@@ -22,15 +22,17 @@ describe CollectionsHelper, :type => :helper do
22
22
  end
23
23
  end
24
24
  describe "button_for_delete_collection" do
25
- before do
25
+ before (:all) do
26
26
  @collection = Collection.create(title: "Test Public")
27
27
  end
28
-
28
+ after (:all) do
29
+ @collection.delete
30
+ end
29
31
  it " should create a button to the collections delete path" do
30
32
  str = button_for_delete_collection @collection
31
33
  doc = Nokogiri::HTML(str)
32
34
  form = doc.xpath('//form').first
33
- expect(form.attr('action')).to eq collections.collection_path(@collection)
35
+ expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
34
36
  i = form.children.first.children[1]
35
37
  expect(i.attr('type')).to eq('submit')
36
38
  end
@@ -38,7 +40,7 @@ describe CollectionsHelper, :type => :helper do
38
40
  str = button_for_delete_collection @collection, "Delete My Button"
39
41
  doc = Nokogiri::HTML(str)
40
42
  form = doc.xpath('//form').first
41
- expect(form.attr('action')).to eq collections.collection_path(@collection)
43
+ expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
42
44
  i = form.children.first.children[1]
43
45
  expect(i.attr('value')).to eq("Delete My Button")
44
46
  end
@@ -53,7 +55,7 @@ describe CollectionsHelper, :type => :helper do
53
55
  str = button_for_remove_from_collection item
54
56
  doc = Nokogiri::HTML(str)
55
57
  form = doc.xpath('//form').first
56
- expect(form.attr('action')).to eq collections.collection_path(@collection)
58
+ expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
57
59
  expect(form.css('input#collection_members[type="hidden"][value="remove"]')).not_to be_empty
58
60
  expect(form.css('input[type="hidden"][name="batch_document_ids[]"][value="changeme:123"]')).not_to be_empty
59
61
  end
@@ -75,34 +77,34 @@ describe CollectionsHelper, :type => :helper do
75
77
  str = button_for_remove_from_collection item
76
78
  doc = Nokogiri::HTML(str)
77
79
  form = doc.xpath('//form').first
78
- expect(form.attr('action')).to eq collections.collection_path(@collection)
80
+ expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
79
81
  expect(form.css('input#collection_members[type="hidden"][value="remove"]')).not_to be_empty
80
82
  expect(form.css('input[type="hidden"][name="batch_document_ids[]"][value="changeme:123"]')).not_to be_empty
81
83
  end
82
84
 
83
85
  end
84
- end
85
-
86
+ end
86
87
  describe "button_for_remove_selected_from_collection" do
87
- before do
88
- @collection = Collection.create title: "Test Public"
88
+ before (:all) do
89
+ @collection = Collection.create title:"Test Public"
90
+ end
91
+ after (:all) do
92
+ @collection.delete
89
93
  end
90
-
91
94
  it " should create a button to the collections delete path" do
92
95
  str = button_for_remove_selected_from_collection @collection
93
96
  doc = Nokogiri::HTML(str)
94
97
  form = doc.xpath('//form').first
95
- expect(form.attr('action')).to eq collections.collection_path(@collection)
98
+ expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
96
99
  i = form.children[2]
97
100
  expect(i.attr('value')).to eq("remove")
98
101
  expect(i.attr('name')).to eq("collection[members]")
99
102
  end
100
-
101
103
  it "should create a button with my text" do
102
104
  str = button_for_remove_selected_from_collection @collection, "Remove My Button"
103
105
  doc = Nokogiri::HTML(str)
104
106
  form = doc.css('form').first
105
- expect(form.attr('action')).to eq collections.collection_path(@collection)
107
+ expect(form.attr('action')).to eq collections.collection_path(@collection.id)
106
108
  expect(form.css('input[type="submit"]').attr('value').value).to eq "Remove My Button"
107
109
  end
108
110
  end
@@ -4,13 +4,11 @@ describe CollectionsSearchHelper, :type => :helper do
4
4
  describe "collection_name" do
5
5
  let (:collection_without_title) { Collection.create() }
6
6
  let (:collection_with_title) { Collection.create(title: "Title of Collection 2") }
7
-
8
7
  it "should return the pid if no title available" do
9
- expect(collection_name(collection_without_title.id)).to eq collection_without_title.id
8
+ expect(collection_name(collection_without_title.pid)).to eq(collection_without_title.pid)
10
9
  end
11
-
12
10
  it "should return the title value associated with the given pid" do
13
- expect(collection_name(collection_with_title.id)).to eq "Title of Collection 2"
11
+ expect(collection_name(collection_with_title.pid)).to eq("Title of Collection 2")
14
12
  end
15
13
  end
16
14
  end
@@ -15,17 +15,17 @@ describe Hydra::Collections::Collectible do
15
15
  @collection1.members << @collectible
16
16
  @collection1.save
17
17
  @collectible.collections << @collection2
18
- reloaded = CollectibleThing.find(@collectible.id)
19
- expect(@collection2.reload.members).to eq [@collectible]
20
- expect(reloaded.collections).to eq [@collection1, @collection2]
18
+ reloaded = CollectibleThing.find(@collectible.pid)
19
+ expect(@collection2.reload.members).to eq([@collectible])
20
+ expect(reloaded.collections).to eq([@collection1, @collection2])
21
21
  end
22
22
  end
23
- describe "index_collection_ids" do
24
- it "should add ids for all associated collections" do
23
+ describe "index_collection_pids" do
24
+ it "should add pids for all associated collections" do
25
25
  @collectible.save
26
26
  @collectible.collections << @collection1
27
27
  @collectible.collections << @collection2
28
- expect(@collectible.index_collection_ids["collection_sim"]).to eq [@collection1.id, @collection2.id]
28
+ expect(@collectible.index_collection_pids["collection_sim"]).to eq([@collection1.pid, @collection2.pid])
29
29
  end
30
30
  end
31
- end
31
+ end
@@ -7,9 +7,9 @@ describe Collection, :type => :model do
7
7
  include Hydra::Collections::Collectible
8
8
 
9
9
  def to_solr(solr_doc={}, opts={})
10
- super(solr_doc, opts).tap do |solr_doc|
11
- solr_doc = index_collection_ids(solr_doc)
12
- end
10
+ super(solr_doc, opts)
11
+ solr_doc = index_collection_pids(solr_doc)
12
+ return solr_doc
13
13
  end
14
14
 
15
15
  end
@@ -18,120 +18,175 @@ describe Collection, :type => :model do
18
18
  @user.destroy
19
19
  Object.send(:remove_const, :GenericFile)
20
20
  end
21
+ let(:gf1) { GenericFile.create }
22
+ let(:gf2) { GenericFile.create }
21
23
 
22
- before do
23
- @collection = Collection.new
24
- @collection.apply_depositor_metadata(@user.user_key)
25
- @collection.save!
26
- @gf1 = GenericFile.create
27
- @gf2 = GenericFile.create
28
- end
24
+ let(:user) { @user }
29
25
 
30
- it "should have a depositor" do
31
- expect(@collection.depositor).to eq(@user.user_key)
32
- end
26
+ describe "#to_solr" do
27
+ let(:collection) { Collection.new(title: "A good title") }
28
+
29
+ subject { collection.to_solr }
33
30
 
34
- it "should allow the depositor to edit and read" do
35
- ability = Ability.new(@user)
36
- expect(ability.can?(:read, @collection)).to be true
37
- expect(ability.can?(:edit, @collection)).to be true
31
+ it "should have title" do
32
+ expect(subject['desc_metadata__title_tesim']).to eq ['A good title']
33
+ end
38
34
  end
39
35
 
40
- it "should be empty by default" do
41
- expect(@collection.members).to be_empty
36
+ describe "#depositor" do
37
+ before do
38
+ subject.apply_depositor_metadata(user)
39
+ end
40
+
41
+ it "should have a depositor" do
42
+ expect(subject.depositor).to eq(user.user_key)
43
+ end
42
44
  end
43
45
 
44
- it "should have many files" do
45
- @collection.members = [@gf1, @gf2]
46
- @collection.save
47
- expect(@collection.reload.members).to match_array [@gf1, @gf2]
46
+ describe "the ability" do
47
+ let(:collection) do
48
+ Collection.new.tap do |collection|
49
+ collection.apply_depositor_metadata(user)
50
+ collection.save
51
+ end
52
+ end
53
+ subject { Ability.new(user) }
54
+
55
+ it "should allow the depositor to edit and read" do
56
+ expect(subject.can?(:read, collection)).to be true
57
+ expect(subject.can?(:edit, collection)).to be true
58
+ end
48
59
  end
49
60
 
50
- it "should allow new files to be added" do
51
- @collection.members = [@gf1]
52
- @collection.save
53
- @collection.reload
54
- @collection.members << @gf2
55
- @collection.save
56
- expect(@collection.reload.members).to match_array [@gf1, @gf2]
61
+ describe "#members" do
62
+ it "should be empty by default" do
63
+ expect(subject.members).to be_empty
64
+ end
65
+
66
+ context "adding members" do
67
+ context "using assignment" do
68
+ subject { Collection.create(members: [gf1, gf2]) }
69
+
70
+ it "should have many files" do
71
+ expect(subject.reload.members).to eq [gf1, gf2]
72
+ end
73
+ end
74
+
75
+ context "using append" do
76
+ before do
77
+ subject.members = [gf1]
78
+ subject.save
79
+ end
80
+ it "should allow new files to be added" do
81
+ subject.reload
82
+ subject.members << gf2
83
+ subject.save
84
+ expect(subject.reload.members).to eq [gf1, gf2]
85
+ end
86
+ end
87
+ end
88
+
89
+
90
+ context "removing members" do
91
+ before do
92
+ subject.members = [gf1, gf2]
93
+ subject.save
94
+ end
95
+
96
+ it "should allow files to be removed" do
97
+ expect(gf1.collections).to eq [subject] # This line forces the "collections" to be cached.
98
+ # We need to ensure that deleting causes the collection to be flushed.
99
+ solr_doc_before_remove = ActiveFedora::SolrInstanceLoader.new(ActiveFedora::Base, gf1.pid).send(:solr_doc)
100
+ expect(solr_doc_before_remove["collection_tesim"]).to eq [subject.pid]
101
+ subject.reload.members.delete(gf1)
102
+ subject.save
103
+ expect(subject.reload.members).to eq [gf2]
104
+ solr_doc_after_remove = ActiveFedora::SolrInstanceLoader.new(ActiveFedora::Base, gf1.pid).send(:solr_doc)
105
+ expect(solr_doc_after_remove["collection_tesim"]).to be_nil
106
+ end
107
+ end
57
108
  end
58
109
 
59
110
  it "should set the date uploaded on create" do
60
- @collection.save
61
- expect(@collection.date_uploaded).to be_kind_of(Date)
62
- end
63
- it "should update the date modified on update" do
64
- uploaded_date = Date.today
65
- modified_date = Date.tomorrow
66
- expect(Date).to receive(:today).twice.and_return(uploaded_date, modified_date)
67
- @collection.save
68
- expect(@collection.date_modified).to eq(uploaded_date)
69
- @collection.members = [@gf1]
70
- @collection.save
71
- expect(@collection.date_modified).to eq(modified_date)
72
- @gf1 = @gf1.reload
73
- expect(@gf1.collections).to include(@collection)
74
- expect(@gf1.to_solr[Solrizer.solr_name(:collection)]).to eq([@collection.id])
111
+ subject.save
112
+ expect(subject.date_uploaded).to be_kind_of(Date)
113
+ end
114
+
115
+ describe "when updating" do
116
+ let(:gf1) { GenericFile.create }
117
+
118
+ it "should update the date modified on update" do
119
+ uploaded_date = Date.today
120
+ modified_date = Date.tomorrow
121
+ subject.save
122
+ allow(Date).to receive(:today).and_return(uploaded_date, modified_date)
123
+ subject.save
124
+ expect(subject.date_modified).to eq uploaded_date
125
+ subject.members = [gf1]
126
+ subject.save
127
+ expect(subject.date_modified).to eq modified_date
128
+ expect(gf1.reload.collections).to include(subject)
129
+ expect(gf1.to_solr[Solrizer.solr_name(:collection)]).to eq [subject.id]
130
+ end
75
131
  end
132
+
76
133
  it "should have a title" do
77
- @collection.title = "title"
78
- @collection.save
79
- expect(Collection.find(@collection.id).title).to eq(@collection.title)
134
+ subject.title = "title"
135
+ subject.save
136
+ expect(subject.title).to eq "title"
80
137
  end
138
+
81
139
  it "should have a description" do
82
- @collection.description = "description"
83
- @collection.save
84
- expect(Collection.find(@collection.id).description).to eq(@collection.description)
140
+ subject.description = "description"
141
+ subject.save
142
+ expect(subject.reload.description).to eq "description"
85
143
  end
86
144
 
87
145
  it "should have the expected display terms" do
88
- expect(@collection.terms_for_display).to include(
89
- :part_of, :contributor, :creator, :title, :description, :publisher,
90
- :date_created, :date_uploaded, :date_modified, :subject, :language, :rights,
91
- :resource_type, :identifier, :based_near, :tag, :related_url
92
- )
146
+ expect(subject.terms_for_display).to eq([:part_of, :contributor, :creator, :title, :description, :publisher, :date_created, :date_uploaded, :date_modified, :subject, :language, :rights, :resource_type, :identifier, :based_near, :tag, :related_url])
93
147
  end
148
+
94
149
  it "should have the expected edit terms" do
95
- expect(@collection.terms_for_editing).to include(
96
- :part_of, :contributor, :creator, :title, :description, :publisher, :date_created,
97
- :subject, :language, :rights, :resource_type, :identifier, :based_near, :tag, :related_url
98
- )
150
+ expect(subject.terms_for_editing).to eq([:part_of, :contributor, :creator, :title, :description, :publisher, :date_created, :subject, :language, :rights, :resource_type, :identifier, :based_near, :tag, :related_url])
99
151
  end
100
- it "should not delete member files when deleted" do
101
- @collection.members = [@gf1, @gf2]
102
- @collection.save
103
- @collection.destroy
104
- expect(GenericFile).to exist(@gf1.id)
105
- expect(GenericFile).to exist(@gf2.id)
152
+
153
+ describe "#destroy" do
154
+ before do
155
+ subject.members = [gf1, gf2]
156
+ subject.save
157
+ subject.destroy
158
+ end
159
+
160
+ it "should not delete member files when deleted" do
161
+ expect(GenericFile.exists?(gf1.pid)).to be true
162
+ expect(GenericFile.exists?(gf2.pid)).to be true
163
+ end
106
164
  end
107
165
 
108
166
  describe "Collection by another name" do
109
- before (:all) do
167
+ before do
110
168
  class OtherCollection < ActiveFedora::Base
111
169
  include Hydra::Collection
170
+ include Hydra::Collections::Collectible
112
171
  end
113
-
114
172
  class Member < ActiveFedora::Base
115
173
  include Hydra::Collections::Collectible
116
174
  end
117
175
  end
118
- after(:all) do
176
+ after do
119
177
  Object.send(:remove_const, :OtherCollection)
120
178
  Object.send(:remove_const, :Member)
121
179
  end
122
180
 
123
- let(:member) { Member.create }
124
- let(:collection) { OtherCollection.new }
125
-
126
- before do
181
+ it "have members that know about the collection" do
182
+ collection = OtherCollection.new
183
+ member = Member.create
127
184
  collection.members << member
128
185
  collection.save
129
- end
130
-
131
- it "have members that know about the collection" do
132
186
  member.reload
133
- expect(member.collections).to eq [collection]
187
+ expect(member.collections).to eq([collection])
188
+ collection.destroy
189
+ member.destroy
134
190
  end
135
191
  end
136
-
137
192
  end