Browse files

Add tests

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

0 comments on commit 5cf1f95

Please sign in to comment.