paperclip 2.4.5 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of paperclip might be problematic. Click here for more details.
- data/.gitignore +22 -0
- data/.travis.yml +13 -0
- data/Appraisals +14 -0
- data/CONTRIBUTING.md +38 -0
- data/Gemfile +5 -0
- data/NEWS +23 -0
- data/README.md +72 -42
- data/Rakefile +1 -46
- data/cucumber/paperclip_steps.rb +6 -0
- data/features/basic_integration.feature +46 -0
- data/features/rake_tasks.feature +63 -0
- data/features/step_definitions/attachment_steps.rb +65 -0
- data/features/step_definitions/html_steps.rb +15 -0
- data/features/step_definitions/rails_steps.rb +182 -0
- data/features/step_definitions/s3_steps.rb +14 -0
- data/features/step_definitions/web_steps.rb +209 -0
- data/features/support/env.rb +8 -0
- data/features/support/fakeweb.rb +3 -0
- data/features/support/fixtures/.boot_config.rb.swo +0 -0
- data/features/support/fixtures/boot_config.txt +15 -0
- data/features/support/fixtures/gemfile.txt +5 -0
- data/features/support/fixtures/preinitializer.txt +20 -0
- data/features/support/paths.rb +28 -0
- data/features/support/rails.rb +46 -0
- data/features/support/selectors.rb +19 -0
- data/gemfiles/rails2.gemfile +9 -0
- data/gemfiles/rails3.gemfile +9 -0
- data/gemfiles/rails3_1.gemfile +9 -0
- data/lib/paperclip.rb +26 -19
- data/lib/paperclip/attachment.rb +123 -109
- data/lib/paperclip/interpolations.rb +7 -4
- data/lib/paperclip/matchers.rb +33 -2
- data/lib/paperclip/missing_attachment_styles.rb +1 -1
- data/lib/paperclip/railtie.rb +5 -0
- data/lib/paperclip/schema.rb +39 -0
- data/lib/paperclip/storage/fog.rb +21 -10
- data/lib/paperclip/storage/s3.rb +107 -40
- data/lib/paperclip/style.rb +13 -5
- data/lib/paperclip/url_generator.rb +64 -0
- data/lib/paperclip/version.rb +1 -1
- data/lib/tasks/paperclip.rake +1 -1
- data/paperclip.gemspec +41 -0
- data/test/.gitignore +1 -0
- data/test/attachment_test.rb +155 -168
- data/test/fixtures/question?mark.png +0 -0
- data/test/helper.rb +24 -1
- data/test/interpolations_test.rb +16 -2
- data/test/paperclip_missing_attachment_styles_test.rb +16 -0
- data/test/paperclip_test.rb +72 -22
- data/test/schema_test.rb +98 -0
- data/test/storage/filesystem_test.rb +2 -2
- data/test/{fog_test.rb → storage/fog_test.rb} +35 -8
- data/test/storage/s3_live_test.rb +63 -13
- data/test/storage/s3_test.rb +394 -91
- data/test/style_test.rb +50 -21
- data/test/support/mock_attachment.rb +22 -0
- data/test/support/mock_interpolator.rb +24 -0
- data/test/support/mock_model.rb +2 -0
- data/test/support/mock_url_generator_builder.rb +27 -0
- data/test/url_generator_test.rb +187 -0
- metadata +307 -125
- data/lib/paperclip/options.rb +0 -78
- data/test/options_test.rb +0 -75
@@ -0,0 +1,63 @@
|
|
1
|
+
Feature: Rake tasks
|
2
|
+
|
3
|
+
Background:
|
4
|
+
Given I generate a new rails application
|
5
|
+
And I run a rails generator to generate a "User" scaffold with "name:string"
|
6
|
+
And I run a paperclip generator to add a paperclip "attachment" to the "User" model
|
7
|
+
And I run a migration
|
8
|
+
And I add the paperclip rake task to a Rails 2.3 application
|
9
|
+
And I add this snippet to the User model:
|
10
|
+
"""
|
11
|
+
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
|
12
|
+
"""
|
13
|
+
|
14
|
+
Scenario: Paperclip refresh thumbnails task
|
15
|
+
When I modify my attachment definition to:
|
16
|
+
"""
|
17
|
+
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
|
18
|
+
:styles => { :medium => "200x200#" }
|
19
|
+
"""
|
20
|
+
And I upload the fixture "5k.png"
|
21
|
+
Then the attachment "medium/5k.png" should have a dimension of 200x200
|
22
|
+
When I modify my attachment definition to:
|
23
|
+
"""
|
24
|
+
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
|
25
|
+
:styles => { :medium => "100x100#" }
|
26
|
+
"""
|
27
|
+
When I successfully run `bundle exec rake paperclip:refresh:thumbnails CLASS=User --trace`
|
28
|
+
Then the attachment "original/5k.png" should exist
|
29
|
+
And the attachment "medium/5k.png" should have a dimension of 100x100
|
30
|
+
|
31
|
+
Scenario: Paperclip refresh metadata task
|
32
|
+
When I upload the fixture "5k.png"
|
33
|
+
And I swap the attachment "original/5k.png" with the fixture "12k.png"
|
34
|
+
And I successfully run `bundle exec rake paperclip:refresh:metadata CLASS=User --trace`
|
35
|
+
Then the attachment should have the same content type as the fixture "12k.png"
|
36
|
+
And the attachment should have the same file size as the fixture "12k.png"
|
37
|
+
|
38
|
+
Scenario: Paperclip refresh missing styles task
|
39
|
+
When I upload the fixture "5k.png"
|
40
|
+
Then the attachment file "original/5k.png" should exist
|
41
|
+
And the attachment file "medium/5k.png" should not exist
|
42
|
+
When I modify my attachment definition to:
|
43
|
+
"""
|
44
|
+
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
|
45
|
+
:styles => { :medium => "200x200#" }
|
46
|
+
"""
|
47
|
+
When I successfully run `bundle exec rake paperclip:refresh:missing_styles --trace`
|
48
|
+
Then the attachment file "original/5k.png" should exist
|
49
|
+
And the attachment file "medium/5k.png" should exist
|
50
|
+
|
51
|
+
Scenario: Paperclip clean task
|
52
|
+
When I upload the fixture "5k.png"
|
53
|
+
And I upload the fixture "12k.png"
|
54
|
+
Then the attachment file "original/5k.png" should exist
|
55
|
+
And the attachment file "original/12k.png" should exist
|
56
|
+
When I modify my attachment definition to:
|
57
|
+
"""
|
58
|
+
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
|
59
|
+
validates_attachment_size :attachment, :less_than => 10.kilobytes
|
60
|
+
"""
|
61
|
+
And I successfully run `bundle exec rake paperclip:clean CLASS=User --trace`
|
62
|
+
Then the attachment file "original/5k.png" should exist
|
63
|
+
But the attachment file "original/12k.png" should not exist
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module AttachmentHelpers
|
2
|
+
def fixture_path(filename)
|
3
|
+
File.expand_path("#{PROJECT_ROOT}/test/fixtures/#{filename}")
|
4
|
+
end
|
5
|
+
|
6
|
+
def attachment_path(filename)
|
7
|
+
File.expand_path("public/system/attachments/#{filename}")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
World(AttachmentHelpers)
|
11
|
+
|
12
|
+
When /^I modify my attachment definition to:$/ do |definition|
|
13
|
+
write_file "app/models/user.rb", <<-FILE
|
14
|
+
class User < ActiveRecord::Base
|
15
|
+
#{definition}
|
16
|
+
end
|
17
|
+
FILE
|
18
|
+
in_current_dir { FileUtils.rm_rf ".rbx" }
|
19
|
+
end
|
20
|
+
|
21
|
+
When /^I upload the fixture "([^"]*)"$/ do |filename|
|
22
|
+
run_simple %(bundle exec #{runner_command} "User.create!(:attachment => File.open('#{fixture_path(filename)}'))")
|
23
|
+
end
|
24
|
+
|
25
|
+
Then /^the attachment "([^"]*)" should have a dimension of (\d+x\d+)$/ do |filename, dimension|
|
26
|
+
in_current_dir do
|
27
|
+
geometry = `identify -format "%wx%h" "#{attachment_path(filename)}"`.strip
|
28
|
+
geometry.should == dimension
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Then /^the attachment "([^"]*)" should exist$/ do |filename|
|
33
|
+
in_current_dir do
|
34
|
+
File.exists?(attachment_path(filename)).should be
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
When /^I swap the attachment "([^"]*)" with the fixture "([^"]*)"$/ do |attachment_filename, fixture_filename|
|
39
|
+
in_current_dir do
|
40
|
+
require 'fileutils'
|
41
|
+
FileUtils.rm_f attachment_path(attachment_filename)
|
42
|
+
FileUtils.cp fixture_path(fixture_filename), attachment_path(attachment_filename)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Then /^the attachment should have the same content type as the fixture "([^"]*)"$/ do |filename|
|
47
|
+
in_current_dir do
|
48
|
+
require 'mime/types'
|
49
|
+
attachment_content_type = `bundle exec #{runner_command} "puts User.last.attachment_content_type"`.strip
|
50
|
+
attachment_content_type.should == MIME::Types.type_for(filename).first.content_type
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Then /^the attachment should have the same file size as the fixture "([^"]*)"$/ do |filename|
|
55
|
+
in_current_dir do
|
56
|
+
attachment_file_size = `bundle exec #{runner_command} "puts User.last.attachment_file_size"`.strip
|
57
|
+
attachment_file_size.should == File.size(fixture_path(filename)).to_s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
Then /^the attachment file "([^"]*)" should (not )?exist$/ do |filename, not_exist|
|
62
|
+
in_current_dir do
|
63
|
+
check_file_presence([attachment_path(filename)], !not_exist)
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Then %r{I should see an image with a path of "([^"]*)"} do |path|
|
2
|
+
page.should have_css("img[src^='#{path}']")
|
3
|
+
end
|
4
|
+
|
5
|
+
Then %r{^the file at "([^"]*)" is the same as "([^"]*)"$} do |web_file, path|
|
6
|
+
expected = IO.read(path)
|
7
|
+
actual = if web_file.match %r{^https?://}
|
8
|
+
Net::HTTP.get(URI.parse(web_file))
|
9
|
+
else
|
10
|
+
visit(web_file)
|
11
|
+
page.body
|
12
|
+
end
|
13
|
+
actual.force_encoding("UTF-8") if actual.respond_to?(:force_encoding)
|
14
|
+
actual.should == expected
|
15
|
+
end
|
@@ -0,0 +1,182 @@
|
|
1
|
+
Given /^I generate a new rails application$/ do
|
2
|
+
steps %{
|
3
|
+
When I run `bundle exec #{new_application_command} #{APP_NAME}`
|
4
|
+
And I cd to "#{APP_NAME}"
|
5
|
+
And I turn off class caching
|
6
|
+
And I write to "Gemfile" with:
|
7
|
+
"""
|
8
|
+
source "http://rubygems.org"
|
9
|
+
gem "rails", "#{framework_version}"
|
10
|
+
gem "sqlite3"
|
11
|
+
gem "capybara"
|
12
|
+
gem "gherkin"
|
13
|
+
gem "aws-sdk"
|
14
|
+
"""
|
15
|
+
And I configure the application to use "paperclip" from this project
|
16
|
+
And I reset Bundler environment variable
|
17
|
+
And I successfully run `bundle install --local`
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
Given /^I run a rails generator to generate a "([^"]*)" scaffold with "([^"]*)"$/ do |model_name, attributes|
|
22
|
+
step %[I successfully run `bundle exec #{generator_command} scaffold #{model_name} #{attributes}`]
|
23
|
+
end
|
24
|
+
|
25
|
+
Given /^I run a paperclip generator to add a paperclip "([^"]*)" to the "([^"]*)" model$/ do |attachment_name, model_name|
|
26
|
+
step %[I successfully run `bundle exec #{generator_command} paperclip #{model_name} #{attachment_name}`]
|
27
|
+
end
|
28
|
+
|
29
|
+
Given /^I run a migration$/ do
|
30
|
+
step %[I successfully run `bundle exec rake db:migrate`]
|
31
|
+
end
|
32
|
+
|
33
|
+
Given /^I update my new user view to include the file upload field$/ do
|
34
|
+
if framework_version?("3")
|
35
|
+
steps %{
|
36
|
+
Given I overwrite "app/views/users/new.html.erb" with:
|
37
|
+
"""
|
38
|
+
<%= form_for @user, :html => { :multipart => true } do |f| %>
|
39
|
+
<%= f.label :name %>
|
40
|
+
<%= f.text_field :name %>
|
41
|
+
<%= f.label :attachment %>
|
42
|
+
<%= f.file_field :attachment %>
|
43
|
+
<%= submit_tag "Submit" %>
|
44
|
+
<% end %>
|
45
|
+
"""
|
46
|
+
}
|
47
|
+
else
|
48
|
+
steps %{
|
49
|
+
Given I overwrite "app/views/users/new.html.erb" with:
|
50
|
+
"""
|
51
|
+
<% form_for @user, :html => { :multipart => true } do |f| %>
|
52
|
+
<%= f.label :name %>
|
53
|
+
<%= f.text_field :name %>
|
54
|
+
<%= f.label :attachment %>
|
55
|
+
<%= f.file_field :attachment %>
|
56
|
+
<%= submit_tag "Submit" %>
|
57
|
+
<% end %>
|
58
|
+
"""
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
Given /^I update my user view to include the attachment$/ do
|
64
|
+
steps %{
|
65
|
+
Given I overwrite "app/views/users/show.html.erb" with:
|
66
|
+
"""
|
67
|
+
<p>Name: <%= @user.name %></p>
|
68
|
+
<p>Attachment: <%= image_tag @user.attachment.url %></p>
|
69
|
+
"""
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
Given /^I add this snippet to the User model:$/ do |snippet|
|
74
|
+
file_name = "app/models/user.rb"
|
75
|
+
in_current_dir do
|
76
|
+
content = File.read(file_name)
|
77
|
+
File.open(file_name, 'w') { |f| f << content.sub(/end\Z/, "#{snippet}\nend") }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
Given /^I start the rails application$/ do
|
82
|
+
in_current_dir do
|
83
|
+
require "./config/environment"
|
84
|
+
require "capybara/rails"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
Given /^I reload my application$/ do
|
89
|
+
Rails::Application.reload!
|
90
|
+
end
|
91
|
+
|
92
|
+
When %r{I turn off class caching} do
|
93
|
+
in_current_dir do
|
94
|
+
file = "config/environments/test.rb"
|
95
|
+
config = IO.read(file)
|
96
|
+
config.gsub!(%r{^\s*config.cache_classes.*$},
|
97
|
+
"config.cache_classes = false")
|
98
|
+
File.open(file, "w"){|f| f.write(config) }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
Given /^I update my application to use Bundler$/ do
|
103
|
+
if framework_version?("2")
|
104
|
+
boot_config_template = File.read('features/support/fixtures/boot_config.txt')
|
105
|
+
preinitializer_template = File.read('features/support/fixtures/preinitializer.txt')
|
106
|
+
gemfile_template = File.read('features/support/fixtures/gemfile.txt')
|
107
|
+
in_current_dir do
|
108
|
+
content = File.read("config/boot.rb").sub(/Rails\.boot!/, boot_config_template)
|
109
|
+
File.open("config/boot.rb", "w") { |file| file.write(content) }
|
110
|
+
File.open("config/preinitializer.rb", "w") { |file| file.write(preinitializer_template) }
|
111
|
+
File.open("Gemfile", "w") { |file| file.write(gemfile_template.sub(/RAILS_VERSION/, framework_version)) }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
Given /^I add the paperclip rake task to a Rails 2.3 application$/ do
|
117
|
+
if framework_version?("2.3")
|
118
|
+
require 'fileutils'
|
119
|
+
source = File.expand_path('lib/tasks/paperclip.rake')
|
120
|
+
destination = in_current_dir { File.expand_path("lib/tasks") }
|
121
|
+
FileUtils.cp source, destination
|
122
|
+
append_to "Rakefile", "require 'paperclip'"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
Then /^the file at "([^"]*)" should be the same as "([^"]*)"$/ do |web_file, path|
|
127
|
+
expected = IO.read(path)
|
128
|
+
actual = if web_file.match %r{^https?://}
|
129
|
+
Net::HTTP.get(URI.parse(web_file))
|
130
|
+
else
|
131
|
+
visit(web_file)
|
132
|
+
page.source
|
133
|
+
end
|
134
|
+
actual.force_encoding("UTF-8") if actual.respond_to?(:force_encoding)
|
135
|
+
actual.should == expected
|
136
|
+
end
|
137
|
+
|
138
|
+
When /^I configure the application to use "([^\"]+)" from this project$/ do |name|
|
139
|
+
append_to_gemfile "gem '#{name}', :path => '#{PROJECT_ROOT}'"
|
140
|
+
steps %{And I run `bundle install --local`}
|
141
|
+
end
|
142
|
+
|
143
|
+
When /^I configure the application to use "([^\"]+)"$/ do |gem_name|
|
144
|
+
append_to_gemfile "gem '#{gem_name}'"
|
145
|
+
end
|
146
|
+
|
147
|
+
When /^I append gems from Appraisal Gemfile$/ do
|
148
|
+
File.read(ENV['BUNDLE_GEMFILE']).split(/\n/).each do |line|
|
149
|
+
if line =~ /^gem "(?!rails|appraisal)/
|
150
|
+
append_to_gemfile line.strip
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
When /^I comment out the gem "([^"]*)" from the Gemfile$/ do |gemname|
|
156
|
+
comment_out_gem_in_gemfile gemname
|
157
|
+
end
|
158
|
+
|
159
|
+
module FileHelpers
|
160
|
+
def append_to(path, contents)
|
161
|
+
in_current_dir do
|
162
|
+
File.open(path, "a") do |file|
|
163
|
+
file.puts
|
164
|
+
file.puts contents
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def append_to_gemfile(contents)
|
170
|
+
append_to('Gemfile', contents)
|
171
|
+
end
|
172
|
+
|
173
|
+
def comment_out_gem_in_gemfile(gemname)
|
174
|
+
in_current_dir do
|
175
|
+
gemfile = File.read("Gemfile")
|
176
|
+
gemfile.sub!(/^(\s*)(gem\s*['"]#{gemname})/, "\\1# \\2")
|
177
|
+
File.open("Gemfile", 'w'){ |file| file.write(gemfile) }
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
World(FileHelpers)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field|
|
2
|
+
definition = User.attachment_definitions[field.downcase.to_sym]
|
3
|
+
path = "https://paperclip.s3.amazonaws.com#{definition[:path]}"
|
4
|
+
path.gsub!(':filename', File.basename(file_path))
|
5
|
+
path.gsub!(/:([^\/\.]+)/) do |match|
|
6
|
+
"([^\/\.]+)"
|
7
|
+
end
|
8
|
+
FakeWeb.register_uri(:put, Regexp.new(path), :body => "OK")
|
9
|
+
step "I attach the file \"#{file_path}\" to \"#{field}\""
|
10
|
+
end
|
11
|
+
|
12
|
+
Then /^the file at "([^"]*)" should be uploaded to S3$/ do |url|
|
13
|
+
FakeWeb.registered_uri?(:put, url)
|
14
|
+
end
|
@@ -0,0 +1,209 @@
|
|
1
|
+
# TL;DR: YOU SHOULD DELETE THIS FILE
|
2
|
+
#
|
3
|
+
# This file was generated by Cucumber-Rails and is only here to get you a head start
|
4
|
+
# These step definitions are thin wrappers around the Capybara/Webrat API that lets you
|
5
|
+
# visit pages, interact with widgets and make assertions about page content.
|
6
|
+
#
|
7
|
+
# If you use these step definitions as basis for your features you will quickly end up
|
8
|
+
# with features that are:
|
9
|
+
#
|
10
|
+
# * Hard to maintain
|
11
|
+
# * Verbose to read
|
12
|
+
#
|
13
|
+
# A much better approach is to write your own higher level step definitions, following
|
14
|
+
# the advice in the following blog posts:
|
15
|
+
#
|
16
|
+
# * http://benmabey.com/2008/05/19/imperative-vs-declarative-scenarios-in-user-stories.html
|
17
|
+
# * http://dannorth.net/2011/01/31/whose-domain-is-it-anyway/
|
18
|
+
# * http://elabs.se/blog/15-you-re-cuking-it-wrong
|
19
|
+
#
|
20
|
+
|
21
|
+
|
22
|
+
require 'uri'
|
23
|
+
require 'cgi'
|
24
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
|
25
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "selectors"))
|
26
|
+
|
27
|
+
module WithinHelpers
|
28
|
+
def with_scope(locator)
|
29
|
+
locator ? within(*selector_for(locator)) { yield } : yield
|
30
|
+
end
|
31
|
+
end
|
32
|
+
World(WithinHelpers)
|
33
|
+
|
34
|
+
# Single-line step scoper
|
35
|
+
When /^(.*) within (.*[^:])$/ do |step, parent|
|
36
|
+
with_scope(parent) { When step }
|
37
|
+
end
|
38
|
+
|
39
|
+
# Multi-line step scoper
|
40
|
+
When /^(.*) within (.*[^:]):$/ do |step, parent, table_or_string|
|
41
|
+
with_scope(parent) { When "#{step}:", table_or_string }
|
42
|
+
end
|
43
|
+
|
44
|
+
Given /^(?:|I )am on (.+)$/ do |page_name|
|
45
|
+
visit path_to(page_name)
|
46
|
+
end
|
47
|
+
|
48
|
+
When /^(?:|I )go to (.+)$/ do |page_name|
|
49
|
+
visit path_to(page_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
When /^(?:|I )press "([^"]*)"$/ do |button|
|
53
|
+
click_button(button)
|
54
|
+
end
|
55
|
+
|
56
|
+
When /^(?:|I )follow "([^"]*)"$/ do |link|
|
57
|
+
click_link(link)
|
58
|
+
end
|
59
|
+
|
60
|
+
When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
|
61
|
+
fill_in(field, :with => value)
|
62
|
+
end
|
63
|
+
|
64
|
+
When /^(?:|I )fill in "([^"]*)" for "([^"]*)"$/ do |value, field|
|
65
|
+
fill_in(field, :with => value)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Use this to fill in an entire form with data from a table. Example:
|
69
|
+
#
|
70
|
+
# When I fill in the following:
|
71
|
+
# | Account Number | 5002 |
|
72
|
+
# | Expiry date | 2009-11-01 |
|
73
|
+
# | Note | Nice guy |
|
74
|
+
# | Wants Email? | |
|
75
|
+
#
|
76
|
+
# TODO: Add support for checkbox, select og option
|
77
|
+
# based on naming conventions.
|
78
|
+
#
|
79
|
+
When /^(?:|I )fill in the following:$/ do |fields|
|
80
|
+
fields.rows_hash.each do |name, value|
|
81
|
+
When %{I fill in "#{name}" with "#{value}"}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
|
86
|
+
select(value, :from => field)
|
87
|
+
end
|
88
|
+
|
89
|
+
When /^(?:|I )check "([^"]*)"$/ do |field|
|
90
|
+
check(field)
|
91
|
+
end
|
92
|
+
|
93
|
+
When /^(?:|I )uncheck "([^"]*)"$/ do |field|
|
94
|
+
uncheck(field)
|
95
|
+
end
|
96
|
+
|
97
|
+
When /^(?:|I )choose "([^"]*)"$/ do |field|
|
98
|
+
choose(field)
|
99
|
+
end
|
100
|
+
|
101
|
+
When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field|
|
102
|
+
attach_file(field, File.expand_path(path))
|
103
|
+
end
|
104
|
+
|
105
|
+
Then /^(?:|I )should see "([^"]*)"$/ do |text|
|
106
|
+
if page.respond_to? :should
|
107
|
+
page.should have_content(text)
|
108
|
+
else
|
109
|
+
assert page.has_content?(text)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp|
|
114
|
+
regexp = Regexp.new(regexp)
|
115
|
+
|
116
|
+
if page.respond_to? :should
|
117
|
+
page.should have_xpath('//*', :text => regexp)
|
118
|
+
else
|
119
|
+
assert page.has_xpath?('//*', :text => regexp)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
Then /^(?:|I )should not see "([^"]*)"$/ do |text|
|
124
|
+
if page.respond_to? :should
|
125
|
+
page.should have_no_content(text)
|
126
|
+
else
|
127
|
+
assert page.has_no_content?(text)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp|
|
132
|
+
regexp = Regexp.new(regexp)
|
133
|
+
|
134
|
+
if page.respond_to? :should
|
135
|
+
page.should have_no_xpath('//*', :text => regexp)
|
136
|
+
else
|
137
|
+
assert page.has_no_xpath?('//*', :text => regexp)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value|
|
142
|
+
with_scope(parent) do
|
143
|
+
field = find_field(field)
|
144
|
+
if field.value.respond_to? :should
|
145
|
+
field.value.should =~ /#{value}/
|
146
|
+
else
|
147
|
+
assert_match(/#{value}/, field.value)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
Then /^the "([^"]*)" field(?: within (.*))? should not contain "([^"]*)"$/ do |field, parent, value|
|
153
|
+
with_scope(parent) do
|
154
|
+
field = find_field(field)
|
155
|
+
if field.value.respond_to? :should_not
|
156
|
+
field.value.should_not =~ /#{value}/
|
157
|
+
else
|
158
|
+
assert_no_match(/#{value}/, field.value)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
Then /^the "([^"]*)" checkbox(?: within (.*))? should be checked$/ do |label, parent|
|
164
|
+
with_scope(parent) do
|
165
|
+
field_checked = find_field(label)['checked']
|
166
|
+
if field_checked.respond_to? :should
|
167
|
+
field_checked.should be_true
|
168
|
+
else
|
169
|
+
assert field_checked
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
Then /^the "([^"]*)" checkbox(?: within (.*))? should not be checked$/ do |label, parent|
|
175
|
+
with_scope(parent) do
|
176
|
+
field_checked = find_field(label)['checked']
|
177
|
+
if field_checked.respond_to? :should
|
178
|
+
field_checked.should be_false
|
179
|
+
else
|
180
|
+
assert !field_checked
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
Then /^(?:|I )should be on (.+)$/ do |page_name|
|
186
|
+
current_path = URI.parse(current_url).path
|
187
|
+
if current_path.respond_to? :should
|
188
|
+
current_path.should == path_to(page_name)
|
189
|
+
else
|
190
|
+
assert_equal path_to(page_name), current_path
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
|
195
|
+
query = URI.parse(current_url).query
|
196
|
+
actual_params = query ? CGI.parse(query) : {}
|
197
|
+
expected_params = {}
|
198
|
+
expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}
|
199
|
+
|
200
|
+
if actual_params.respond_to? :should
|
201
|
+
actual_params.should == expected_params
|
202
|
+
else
|
203
|
+
assert_equal expected_params, actual_params
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
Then /^show me the page$/ do
|
208
|
+
save_and_open_page
|
209
|
+
end
|