Skip to content
This repository
Browse code

Add tests

* Ensure correct view is selected when mobile vs Html is used.
  • Loading branch information...
commit 5cf1f957ae3028b743113df5ffcf6f4f6aeece97 1 parent ecc209a
peakpg peakpg authored
11 Gemfile
@@ -4,4 +4,15 @@ gem 'rails', '3.0.11'
4 4
5 5 gem 'sqlite3'
6 6
  7 +
  8 +group :test do
  9 + gem 'cucumber-rails'
  10 + gem 'database_cleaner'
  11 + # gem 'cucumber-browsercms' # This is what I want
  12 + gem 'bcms_support', :path=>"~/projects/bcms_support"
  13 +
  14 + gem 'launchy'
  15 + gem 'factory_girl_rails'
  16 +end
  17 +
7 18 gemspec
1  README → README.markdown
Source Rendered
@@ -13,6 +13,7 @@ Caveats:
13 13 ## Issues
14 14 * What happens if I don't create a mobile template for a page.
15 15 * If .mobile view exists, but no .mobile template, it renders with no layout. (Does not fall back to .html)
  16 +* Not 100% sure why admins ALWAYS see the desktop template (even when viewing as mobile). Probably related to how ContentController stack changes.
16 17
17 18 ## Features (Possible)
18 19
8 Rakefile
@@ -4,4 +4,12 @@
4 4 require File.expand_path('../config/application', __FILE__)
5 5 require 'rake'
6 6
  7 +require 'bundler'
  8 +Bundler::GemHelper.install_tasks
  9 +
7 10 BcmsMobile::Application.load_tasks
  11 +
  12 +# Cucumber tasks will run twice.
  13 +# This issue might be the problem, but the suggested fix didn't work: http://andymaleh.blogspot.com/2011/09/cucumber-tests-run-twice-in-rails-3.html until BrowserCMS
  14 +#
  15 +task :test => ['test:units', 'test:functionals', 'test:integration', 'cucumber']
1  app/views/layouts/templates/default.html.erb
@@ -9,6 +9,7 @@
9 9 <%= cms_toolbar %>
10 10 <div id="wrapper" style="width: 700px; margin: 0 auto; text-align: left; padding: 30px">
11 11 I am the desktop TEMPLATE.
  12 + User Agent '<%= request.user_agent %>'
12 13 <%= render_menu %>
13 14 <h1><%= page_title %></h1>
14 15 <%= container :main %>
2  app/views/layouts/templates/default.mobile.erb
@@ -8,7 +8,7 @@
8 8 <body style="margin: 0; padding: 0; text-align: center;">
9 9 <%= cms_toolbar %>
10 10 <div id="wrapper" style="width: 700px; margin: 0 auto; text-align: left; padding: 30px">
11   - I am a stripped down <b>MOBILE</b> template.
  11 + I am a stripped down MOBILE template.
12 12 <h1><%= page_title %></h1>
13 13 <%= container :main %>
14 14 </div>
6 bcms_mobile.gemspec
... ... @@ -1,7 +1,11 @@
  1 +# -*- encoding: utf-8 -*-
  2 +$:.push File.expand_path("../lib", __FILE__)
  3 +require "bcms_mobile/version"
  4 +
1 5 Gem::Specification.new do |spec|
2 6 spec.name = "bcms_mobile"
3 7 spec.rubyforge_project = spec.name
4   - spec.version = "1.0.0"
  8 + spec.version = BcmsMobile::VERSION
5 9 spec.summary = "A Bcms Mobile Module for BrowserCMS"
6 10 spec.author = "BrowserMedia"
7 11 spec.email = "github@browsermedia.com"
8 config/cucumber.yml
... ... @@ -0,0 +1,8 @@
  1 +<%
  2 +rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
  3 +rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
  4 +std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
  5 +%>
  6 +default: <%= std_opts %> features
  7 +wip: --tags @wip:3 --wip features
  8 +rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
5 config/database.yml
@@ -9,7 +9,7 @@ development:
9 9 # Warning: The database defined as "test" will be erased and
10 10 # re-generated from your development database when you run "rake".
11 11 # Do not set this db to the same as development or production.
12   -test:
  12 +test: &test
13 13 adapter: sqlite3
14 14 database: db/test.sqlite3
15 15 pool: 5
@@ -20,3 +20,6 @@ production:
20 20 database: db/production.sqlite3
21 21 pool: 5
22 22 timeout: 5000
  23 +
  24 +cucumber:
  25 + <<: *test
20 features/mobile_templates.feature
... ... @@ -0,0 +1,20 @@
  1 +Feature:
  2 + Visitors on mobile devices should see mobile templates when they visit pages.
  3 +
  4 + Background:
  5 +
  6 + Scenario: Verify CMS seed data is loaded
  7 + Then the homepage should exist
  8 +
  9 + Scenario: Desktop Visitor sees Desktop template
  10 + Given a page exists at /mobile-page
  11 + When a desktop user requests /mobile-page
  12 + Then the page should use the desktop template
  13 +
  14 + Scenario: Mobile visitor sees mobile template
  15 + Given a page exists at /mobile-page
  16 + When a mobile user requests /mobile-page
  17 + Then the page should use the mobile template
  18 +
  19 +
  20 +
33 features/step_definitions/mobile_steps.rb
... ... @@ -0,0 +1,33 @@
  1 +Given /^the homepage should exist$/ do
  2 + assert_equal "Home", Page.find_by_path("/").name
  3 +end
  4 +
  5 +Given /^a page exists at \/mobile\-page$/ do
  6 + @page = Factory(:published_page, :path=>"/mobile-page")
  7 + content = Factory(:html_block, :content=>"Mobile Content")
  8 + @page.add_content(content)
  9 + @page.publish!
  10 +end
  11 +
  12 +When /^a desktop user requests \/mobile\-page$/ do
  13 + visit '/mobile-page'
  14 +end
  15 +
  16 +Then /^the page should use the desktop template$/ do
  17 + assert page.has_content? "I am the desktop TEMPLATE"
  18 + assert page.has_content? "Mobile Content"
  19 +end
  20 +
  21 +When /^a mobile user requests \/mobile\-page$/ do
  22 + request_as_iphone
  23 + visit '/mobile-page'
  24 +end
  25 +
  26 +Then /^the page should use the mobile template$/ do
  27 + assert page.has_content? "I am a stripped down MOBILE template."
  28 + assert page.has_content? "Mobile Content"
  29 +end
  30 +
  31 +Then /^show me the page$/ do
  32 + save_and_open_page
  33 +end
10 features/support/browsercms_testing.rb
... ... @@ -0,0 +1,10 @@
  1 +require "bcms_support/cucumber"
  2 +require "bcms_support/factories"
  3 +
  4 +# Load the BrowserCMS Seed data once at the begin of the tests.
  5 +# Assumes the transaction strategy is used and the seed data will be retained between tests.
  6 +DatabaseCleaner.clean_with(:truncation)
  7 +silence_stream(STDOUT) do
  8 + require File.join(File.dirname(__FILE__), '../../db/seeds.rb')
  9 +end
  10 +
56 features/support/env.rb
... ... @@ -0,0 +1,56 @@
  1 +# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
  2 +# It is recommended to regenerate this file in the future when you upgrade to a
  3 +# newer version of cucumber-rails. Consider adding your own code to a new file
  4 +# instead of editing this one. Cucumber will automatically load all features/**/*.rb
  5 +# files.
  6 +
  7 +require 'cucumber/rails'
  8 +
  9 +# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
  10 +# order to ease the transition to Capybara we set the default here. If you'd
  11 +# prefer to use XPath just remove this line and adjust any selectors in your
  12 +# steps to use the XPath syntax.
  13 +Capybara.default_selector = :css
  14 +
  15 +# By default, any exception happening in your Rails application will bubble up
  16 +# to Cucumber so that your scenario will fail. This is a different from how
  17 +# your application behaves in the production environment, where an error page will
  18 +# be rendered instead.
  19 +#
  20 +# Sometimes we want to override this default behaviour and allow Rails to rescue
  21 +# exceptions and display an error page (just like when the app is running in production).
  22 +# Typical scenarios where you want to do this is when you test your error pages.
  23 +# There are two ways to allow Rails to rescue exceptions:
  24 +#
  25 +# 1) Tag your scenario (or feature) with @allow-rescue
  26 +#
  27 +# 2) Set the value below to true. Beware that doing this globally is not
  28 +# recommended as it will mask a lot of errors for you!
  29 +#
  30 +ActionController::Base.allow_rescue = false
  31 +
  32 +# Remove/comment out the lines below if your app doesn't have a database.
  33 +# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
  34 +begin
  35 + DatabaseCleaner.strategy = :transaction
  36 +rescue NameError
  37 + raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
  38 +end
  39 +
  40 +# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
  41 +# See the DatabaseCleaner documentation for details. Example:
  42 +#
  43 +# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
  44 +# DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
  45 +# end
  46 +#
  47 +# Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
  48 +# DatabaseCleaner.strategy = :transaction
  49 +# end
  50 +#
  51 +
  52 +# Possible values are :truncation and :transaction
  53 +# The :transaction strategy is faster, but might give you threading problems.
  54 +# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature
  55 +Cucumber::Rails::Database.javascript_strategy = :truncation
  56 +
26 features/support/user_agent_mixin.rb
... ... @@ -0,0 +1,26 @@
  1 +SAMPLE_AGENT_STRING = {
  2 + "iPhone" => "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
  3 + "Android" => "HTC_Eris Mozilla/5.0 (Linux; U; Android 4.0; en-ca; Build/GINGERBREAD) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1",
  4 + "Windows Mobile" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; HTC_Touch_Diamond2_T5353; Windows Phone 6.5)",
  5 + "Windows Phone 7" => "Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) Asus;Galaxy6",
  6 + "Blackberry" => "BlackBerry9700/5.0.0.351 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/123",
  7 + "Palm Pre" => "Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0",
  8 + "Nokia N97" => "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124"
  9 +}
  10 +
  11 +module BcmsMobile
  12 + module RackTestDriver
  13 + module UserAgent
  14 +
  15 + # This may break badly with future versions of RackTest.
  16 + def request_as_iphone
  17 + options = page.driver.instance_variable_get("@options")
  18 + options[:headers] = {"HTTP_USER_AGENT" => SAMPLE_AGENT_STRING['iPhone']}
  19 + page.driver.instance_variable_set "@options", options
  20 + end
  21 +
  22 + end
  23 + end
  24 +
  25 +end
  26 +World(BcmsMobile::RackTestDriver::UserAgent)
33 lib/bcms_mobile/engine.rb
@@ -7,6 +7,7 @@ class Engine < Rails::Engine
7 7 include Cms::Module
8 8
9 9 config.to_prepare do
  10 +
10 11 Cms::ContentController.class_eval do
11 12 include BcmsMobile::MobileAware
12 13
@@ -16,15 +17,15 @@ class Engine < Rails::Engine
16 17
17 18 def print_user_agent
18 19 logger.warn "*" * 20
19   - logger.warn "Is mobile request." if respond_as_mobile?
  20 + #logger.warn "Is mobile request." if respond_as_mobile?
20 21 logger.warn "User Agent: #{request.user_agent}"
21 22 end
22 23
23 24 def handle_mobile
24   - w "respond_as_mobile? '#{respond_as_mobile?}'"
25   - w "Before: #{request.formats}"
  25 + #w "respond_as_mobile? '#{respond_as_mobile?}'"
  26 + #w "Before: #{request.formats}"
26 27 super
27   - w "After Formats? '#{request.formats}'"
  28 + #w "After Formats? '#{request.formats}'"
28 29 @handled_mobile = true
29 30 end
30 31
@@ -35,24 +36,36 @@ def handle_mobile
35 36 def render_page
36 37 handle_mobile && print_user_agent unless handle_mobile_called?
37 38
38   - w "**** Override"
39   - w "Request Formats = #{request.formats}"
  39 + m = "Mobile Request?: "
40 40 if respond_as_mobile?
41   - w "Is mobile request"
  41 + m += "Yes"
42 42 else
43   - w "Not Mobile"
  43 + m += "No"
44 44 end
  45 + w m
45 46
46   - w "Mime::Type #{Mime::Type.lookup_by_extension(request.parameters[:format]).to_yaml}"
  47 + mime_type = Mime::Type.lookup_by_extension(request.parameters[:format])
  48 + w "Mime::Type: :#{mime_type.symbol}" if mime_type
47 49 @_page_route.execute(self) if @_page_route
48 50 prepare_connectables_for_render
49 51
50 52 layout = @page.layout
51 53 w "layout = #{layout}"
52 54
53   - render :layout => layout, :action => 'show'
  55 + respond_to do |format|
  56 + format.mobile {
  57 + w "I think this is a mobile request"
  58 + render :layout => layout, :action => 'show'
  59 + }
  60 + format.html {
  61 + w "I think this is an HTML request"
  62 + render :layout => layout, :action => 'show'
  63 + }
  64 + end
  65 + #render :layout => layout, :action => 'show'
54 66 end
55 67
  68 +
56 69 # Because of how BrowserCMS's Content Controller's filter chain works,
57 70 # handle_mobile will not be called when users are logged in. So we need explicitly call it before rendering the page
58 71 def handle_mobile_called?
7 lib/bcms_mobile/mobile_aware.rb
@@ -5,6 +5,11 @@ def w(m)
5 5 logger.warn m
6 6 end
7 7
8   -
  8 + def banner(m)
  9 + w "*" * 20
  10 + w m
  11 + end
9 12 end
  13 +
  14 +
10 15 end
5 lib/tasks/build_gem.rake
... ... @@ -1,5 +0,0 @@
1   -# Add any tasks from your Rake file here (if you were using, say, Jeweler to build gems)
2   -
3   -# Otherwise, this enabled Bundler to build your gem
4   -require 'bundler'
5   -Bundler::GemHelper.install_tasks
65 lib/tasks/cucumber.rake
... ... @@ -0,0 +1,65 @@
  1 +# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
  2 +# It is recommended to regenerate this file in the future when you upgrade to a
  3 +# newer version of cucumber-rails. Consider adding your own code to a new file
  4 +# instead of editing this one. Cucumber will automatically load all features/**/*.rb
  5 +# files.
  6 +
  7 +
  8 +unless ARGV.any? {|a| a =~ /^gems/} # Don't load anything when running the gems:* tasks
  9 +
  10 +vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
  11 +$LOAD_PATH.unshift(File.dirname(vendored_cucumber_bin) + '/../lib') unless vendored_cucumber_bin.nil?
  12 +
  13 +begin
  14 + require 'cucumber/rake/task'
  15 +
  16 + namespace :cucumber do
  17 + Cucumber::Rake::Task.new({:ok => 'db:test:prepare'}, 'Run features that should pass') do |t|
  18 + t.binary = vendored_cucumber_bin # If nil, the gem's binary is used.
  19 + t.fork = true # You may get faster startup if you set this to false
  20 + t.profile = 'default'
  21 + end
  22 +
  23 + Cucumber::Rake::Task.new({:wip => 'db:test:prepare'}, 'Run features that are being worked on') do |t|
  24 + t.binary = vendored_cucumber_bin
  25 + t.fork = true # You may get faster startup if you set this to false
  26 + t.profile = 'wip'
  27 + end
  28 +
  29 + Cucumber::Rake::Task.new({:rerun => 'db:test:prepare'}, 'Record failing features and run only them if any exist') do |t|
  30 + t.binary = vendored_cucumber_bin
  31 + t.fork = true # You may get faster startup if you set this to false
  32 + t.profile = 'rerun'
  33 + end
  34 +
  35 + desc 'Run all features'
  36 + task :all => [:ok, :wip]
  37 +
  38 + task :statsetup do
  39 + require 'rails/code_statistics'
  40 + ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
  41 + ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
  42 + end
  43 + end
  44 + desc 'Alias for cucumber:ok'
  45 + task :cucumber => 'cucumber:ok'
  46 +
  47 + task :default => :cucumber
  48 +
  49 + task :features => :cucumber do
  50 + STDERR.puts "*** The 'features' task is deprecated. See rake -T cucumber ***"
  51 + end
  52 +
  53 + # In case we don't have ActiveRecord, append a no-op task that we can depend upon.
  54 + task 'db:test:prepare' do
  55 + end
  56 +
  57 + task :stats => 'cucumber:statsetup'
  58 +rescue LoadError
  59 + desc 'cucumber rake task not available (cucumber not installed)'
  60 + task :cucumber do
  61 + abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
  62 + end
  63 +end
  64 +
  65 +end
10 script/cucumber
... ... @@ -0,0 +1,10 @@
  1 +#!/usr/bin/env ruby
  2 +
  3 +vendored_cucumber_bin = Dir["#{File.dirname(__FILE__)}/../vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
  4 +if vendored_cucumber_bin
  5 + load File.expand_path(vendored_cucumber_bin)
  6 +else
  7 + require 'rubygems' unless ENV['NO_RUBYGEMS']
  8 + require 'cucumber'
  9 + load Cucumber::BINARY
  10 +end
10 test/factories.rb
... ... @@ -0,0 +1,10 @@
  1 +Factory.define :public_section, :parent => :section do |s|
  2 + s.after_create { |section|
  3 + section.allow_groups = :all
  4 + }
  5 +end
  6 +
  7 +Factory.define :published_page, :parent=>:page do |m|
  8 + m.publish_on_save true
  9 + m.association :section, :factory=>:public_section
  10 +end

0 comments on commit 5cf1f95

Please sign in to comment.
Something went wrong with that request. Please try again.