hydra-collections 3.0.0.beta3 → 4.0.0.beta1

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={}, opts={})
18
- super(solr_doc, opts)
19
- solr_doc = index_collection_pids(solr_doc)
20
- return solr_doc
17
+ def to_solr(solr_doc={})
18
+ super.tap do |solr_doc|
19
+ index_collection_ids(solr_doc)
20
+ end
21
21
  end
22
-
23
22
  end
23
+
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={}, opts={})
29
- super(solr_doc, opts)
30
- solr_doc = index_collection_pids(solr_doc)
31
- return solr_doc
28
+ def to_solr(solr_doc={})
29
+ super.tap do |solr_doc|
30
+ index_collection_ids(solr_doc)
31
+ end
32
32
  end
33
33
 
34
34
  end
@@ -75,12 +75,10 @@ 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.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
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)
84
82
  end
85
83
  end
86
84
 
@@ -24,7 +24,7 @@ describe SelectsCollectionsController, :type => :controller do
24
24
  end
25
25
 
26
26
  describe "Select Collections" do
27
- before (:all) do
27
+ before 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,36 +41,34 @@ 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
55
44
  end
45
+
56
46
  describe "Public Access" do
57
- before (:each) do
47
+ let(:user_collections) do
58
48
  subject.find_collections
59
- @user_collections = subject.instance_variable_get (:@user_collections)
60
- expect(@user_collections).to be_kind_of(Array)
49
+ subject.instance_variable_get(:@user_collections)
61
50
  end
62
- it "should return public collections" do
63
- expect(@user_collections.index{|d| d.id == @collection.id}).not_to be_nil
51
+
52
+ it "should only return public collections" do
53
+ expect(user_collections.map(&:id)).to match_array [@collection.id]
64
54
  end
65
- it "should return all public collections" do
66
- expect(@user_collections.count).to eq(12)
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
67
69
  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
73
70
  end
71
+
74
72
  describe "Read Access" do
75
73
  describe "not signed in" do
76
74
  it "should error if the user is not signed in" do
@@ -78,45 +76,38 @@ describe SelectsCollectionsController, :type => :controller do
78
76
  end
79
77
  end
80
78
  describe "signed in" do
81
- before (:each) do
82
- sign_in @user
79
+ before { sign_in @user }
80
+
81
+ let(:user_collections) do
83
82
  subject.find_collections_with_read_access
84
- @user_collections = subject.instance_variable_get (:@user_collections)
85
- expect(@user_collections).to be_kind_of(Array)
83
+ subject.instance_variable_get(:@user_collections)
86
84
  end
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
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]
91
88
  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
95
89
  end
96
90
  end
91
+
97
92
  describe "Edit Access" do
98
93
  describe "not signed in" do
99
94
  it "should error if the user is not signed in" do
100
95
  expect { subject.find_collections_with_edit_access }.to raise_error
101
96
  end
102
97
  end
98
+
103
99
  describe "signed in" do
104
- before (:each) do
105
- sign_in @user
100
+ before { sign_in @user }
101
+
102
+ let(:user_collections) do
106
103
  subject.find_collections_with_edit_access
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
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
104
+ subject.instance_variable_get(:@user_collections)
117
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]
109
+ end
118
110
  end
119
111
  end
120
112
  end
121
-
122
113
  end
data/spec/factories.rb CHANGED
@@ -1,17 +1,3 @@
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
-
15
1
  FactoryGirl.define do
16
2
  factory :collection
17
3
  end
@@ -1,17 +1,3 @@
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
-
15
1
  FactoryGirl.define do
16
2
  factory :user, :class => User do |u|
17
3
  email 'jilluser@example.com'
@@ -22,17 +22,15 @@ describe CollectionsHelper, :type => :helper do
22
22
  end
23
23
  end
24
24
  describe "button_for_delete_collection" do
25
- before (:all) do
25
+ before do
26
26
  @collection = Collection.create(title: "Test Public")
27
27
  end
28
- after (:all) do
29
- @collection.delete
30
- end
28
+
31
29
  it " should create a button to the collections delete path" do
32
30
  str = button_for_delete_collection @collection
33
31
  doc = Nokogiri::HTML(str)
34
32
  form = doc.xpath('//form').first
35
- expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
33
+ expect(form.attr('action')).to eq collections.collection_path(@collection)
36
34
  i = form.children.first.children[1]
37
35
  expect(i.attr('type')).to eq('submit')
38
36
  end
@@ -40,7 +38,7 @@ describe CollectionsHelper, :type => :helper do
40
38
  str = button_for_delete_collection @collection, "Delete My Button"
41
39
  doc = Nokogiri::HTML(str)
42
40
  form = doc.xpath('//form').first
43
- expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
41
+ expect(form.attr('action')).to eq collections.collection_path(@collection)
44
42
  i = form.children.first.children[1]
45
43
  expect(i.attr('value')).to eq("Delete My Button")
46
44
  end
@@ -55,7 +53,7 @@ describe CollectionsHelper, :type => :helper do
55
53
  str = button_for_remove_from_collection item
56
54
  doc = Nokogiri::HTML(str)
57
55
  form = doc.xpath('//form').first
58
- expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
56
+ expect(form.attr('action')).to eq collections.collection_path(@collection)
59
57
  expect(form.css('input#collection_members[type="hidden"][value="remove"]')).not_to be_empty
60
58
  expect(form.css('input[type="hidden"][name="batch_document_ids[]"][value="changeme:123"]')).not_to be_empty
61
59
  end
@@ -77,34 +75,34 @@ describe CollectionsHelper, :type => :helper do
77
75
  str = button_for_remove_from_collection item
78
76
  doc = Nokogiri::HTML(str)
79
77
  form = doc.xpath('//form').first
80
- expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
78
+ expect(form.attr('action')).to eq collections.collection_path(@collection)
81
79
  expect(form.css('input#collection_members[type="hidden"][value="remove"]')).not_to be_empty
82
80
  expect(form.css('input[type="hidden"][name="batch_document_ids[]"][value="changeme:123"]')).not_to be_empty
83
81
  end
84
82
 
85
83
  end
86
- end
84
+ end
85
+
87
86
  describe "button_for_remove_selected_from_collection" do
88
- before (:all) do
89
- @collection = Collection.create title:"Test Public"
90
- end
91
- after (:all) do
92
- @collection.delete
87
+ before do
88
+ @collection = Collection.create title: "Test Public"
93
89
  end
90
+
94
91
  it " should create a button to the collections delete path" do
95
92
  str = button_for_remove_selected_from_collection @collection
96
93
  doc = Nokogiri::HTML(str)
97
94
  form = doc.xpath('//form').first
98
- expect(form.attr('action')).to eq("#{collections.collection_path(@collection.pid)}")
95
+ expect(form.attr('action')).to eq collections.collection_path(@collection)
99
96
  i = form.children[2]
100
97
  expect(i.attr('value')).to eq("remove")
101
98
  expect(i.attr('name')).to eq("collection[members]")
102
99
  end
100
+
103
101
  it "should create a button with my text" do
104
102
  str = button_for_remove_selected_from_collection @collection, "Remove My Button"
105
103
  doc = Nokogiri::HTML(str)
106
104
  form = doc.css('form').first
107
- expect(form.attr('action')).to eq collections.collection_path(@collection.id)
105
+ expect(form.attr('action')).to eq collections.collection_path(@collection)
108
106
  expect(form.css('input[type="submit"]').attr('value').value).to eq "Remove My Button"
109
107
  end
110
108
  end
@@ -4,11 +4,13 @@ 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
+
7
8
  it "should return the pid if no title available" do
8
- expect(collection_name(collection_without_title.pid)).to eq(collection_without_title.pid)
9
+ expect(collection_name(collection_without_title.id)).to eq collection_without_title.id
9
10
  end
11
+
10
12
  it "should return the title value associated with the given pid" do
11
- expect(collection_name(collection_with_title.pid)).to eq("Title of Collection 2")
13
+ expect(collection_name(collection_with_title.id)).to eq "Title of Collection 2"
12
14
  end
13
15
  end
14
16
  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.pid)
19
- expect(@collection2.reload.members).to eq([@collectible])
20
- expect(reloaded.collections).to eq([@collection1, @collection2])
18
+ reloaded = CollectibleThing.find(@collectible.id)
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_pids" do
24
- it "should add pids for all associated collections" do
23
+ describe "index_collection_ids" do
24
+ it "should add ids for all associated collections" do
25
25
  @collectible.save
26
26
  @collectible.collections << @collection1
27
27
  @collectible.collections << @collection2
28
- expect(@collectible.index_collection_pids["collection_sim"]).to eq([@collection1.pid, @collection2.pid])
28
+ expect(@collectible.index_collection_ids["collection_sim"]).to eq [@collection1.id, @collection2.id]
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)
11
- solr_doc = index_collection_pids(solr_doc)
12
- return solr_doc
10
+ super(solr_doc, opts).tap do |solr_doc|
11
+ solr_doc = index_collection_ids(solr_doc)
12
+ end
13
13
  end
14
14
 
15
15
  end
@@ -18,175 +18,120 @@ 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 }
23
-
24
- let(:user) { @user }
25
-
26
- describe "#to_solr" do
27
- let(:collection) { Collection.new(title: "A good title") }
28
-
29
- subject { collection.to_solr }
30
21
 
31
- it "should have title" do
32
- expect(subject['desc_metadata__title_tesim']).to eq ['A good title']
33
- end
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
34
28
  end
35
29
 
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
30
+ it "should have a depositor" do
31
+ expect(@collection.depositor).to eq(@user.user_key)
44
32
  end
45
33
 
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
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
59
38
  end
60
39
 
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
-
40
+ it "should be empty by default" do
41
+ expect(@collection.members).to be_empty
42
+ end
89
43
 
90
- context "removing members" do
91
- before do
92
- subject.members = [gf1, gf2]
93
- subject.save
94
- end
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]
48
+ end
95
49
 
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
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]
108
57
  end
109
58
 
110
59
  it "should set the date uploaded on create" do
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
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])
131
75
  end
132
-
133
76
  it "should have a title" do
134
- subject.title = "title"
135
- subject.save
136
- expect(subject.title).to eq "title"
77
+ @collection.title = "title"
78
+ @collection.save
79
+ expect(Collection.find(@collection.id).title).to eq(@collection.title)
137
80
  end
138
-
139
81
  it "should have a description" do
140
- subject.description = "description"
141
- subject.save
142
- expect(subject.reload.description).to eq "description"
82
+ @collection.description = "description"
83
+ @collection.save
84
+ expect(Collection.find(@collection.id).description).to eq(@collection.description)
143
85
  end
144
86
 
145
87
  it "should have the expected display terms" do
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])
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
+ )
147
93
  end
148
-
149
94
  it "should have the expected edit terms" do
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])
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
+ )
151
99
  end
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
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)
164
106
  end
165
107
 
166
108
  describe "Collection by another name" do
167
- before do
109
+ before (:all) do
168
110
  class OtherCollection < ActiveFedora::Base
169
111
  include Hydra::Collection
170
- include Hydra::Collections::Collectible
171
112
  end
113
+
172
114
  class Member < ActiveFedora::Base
173
115
  include Hydra::Collections::Collectible
174
116
  end
175
117
  end
176
- after do
118
+ after(:all) do
177
119
  Object.send(:remove_const, :OtherCollection)
178
120
  Object.send(:remove_const, :Member)
179
121
  end
180
122
 
181
- it "have members that know about the collection" do
182
- collection = OtherCollection.new
183
- member = Member.create
123
+ let(:member) { Member.create }
124
+ let(:collection) { OtherCollection.new }
125
+
126
+ before do
184
127
  collection.members << member
185
128
  collection.save
129
+ end
130
+
131
+ it "have members that know about the collection" do
186
132
  member.reload
187
- expect(member.collections).to eq([collection])
188
- collection.destroy
189
- member.destroy
133
+ expect(member.collections).to eq [collection]
190
134
  end
191
135
  end
136
+
192
137
  end