From 00299ccc37e71f5a8f31b510c1f5b617aa9dfca0 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Wed, 19 Feb 2014 13:32:53 -0300 Subject: [PATCH] rails3: fix integration tests --- app/helpers/application_helper.rb | 4 ++-- app/models/category.rb | 2 +- app/views/layouts/_javascript.html.erb | 2 +- config/routes.rb | 2 +- public/javascripts/rails.js | 398 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/integration/assigning_validator_organizations_to_regions_test.rb | 4 ++-- test/integration/blocks_test.rb | 2 +- test/integration/categories_menu_test.rb | 4 +++- test/integration/enable_disable_features_test.rb | 1 - test/integration/manage_documents_test.rb | 2 +- test/integration/routing_test.rb | 6 ++---- test/integration/tiny_mce_languages_test.rb | 2 +- 12 files changed, 413 insertions(+), 16 deletions(-) create mode 100644 public/javascripts/rails.js diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0d28634..16279d3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -678,7 +678,7 @@ module ApplicationHelper lightbox_link_to ''+ _('Search'), { :controller => 'search', :action => 'popup', - :category_path => (@category ? @category.explode_path : []) }, + :category_path => (@category ? @category.explode_path : nil)}, :id => 'open_search' end end @@ -1073,7 +1073,7 @@ module ApplicationHelper links.push(_('New content') => colorbox_options({:href => url_for({:controller => 'cms', :action => 'new', :profile => current_user.login, :cms => true})})) end - link_to(content_tag(:span, _('Contents'), :class => 'icon-menu-articles'), {:controller => "search", :action => 'contents', :category_path => ''}, :id => 'submenu-contents') + + link_to(content_tag(:span, _('Contents'), :class => 'icon-menu-articles'), {:controller => "search", :action => 'contents', :category_path => nil}, :id => 'submenu-contents') + link_to(content_tag(:span, _('Contents menu')), '#', :onclick => "toggleSubmenu(this,'',#{j links.to_json}); return false", :class => 'menu-submenu-trigger up', :id => 'submenu-contents-trigger') end alias :browse_contents_menu :search_contents_menu diff --git a/app/models/category.rb b/app/models/category.rb index 61452a3..11da1a8 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,6 +1,6 @@ class Category < ActiveRecord::Base - attr_accessible :name, :parent_id, :display_color, :display_in_menu, :image_builder, :environment + attr_accessible :name, :parent_id, :display_color, :display_in_menu, :image_builder, :environment, :parent SEARCHABLE_FIELDS = { :name => 10, diff --git a/app/views/layouts/_javascript.html.erb b/app/views/layouts/_javascript.html.erb index a9adffa..4faeacd 100644 --- a/app/views/layouts/_javascript.html.erb +++ b/app/views/layouts/_javascript.html.erb @@ -3,7 +3,7 @@ 'jquery-ui-1.8.2.custom.min', 'jquery.scrollTo', 'jquery.form.js', 'jquery-validation/jquery.validate', 'jquery.cookie', 'jquery.ba-bbq.min.js', 'reflection', 'jquery.tokeninput', 'add-and-join', 'report-abuse', 'catalog', 'manage-products', -'jquery-ui-timepicker-addon', 'application.js', :cache => 'cache-general' %> +'jquery-ui-timepicker-addon', 'application.js', 'rails.js', :cache => 'cache-general' %> <% language = FastGettext.locale %> <% %w{messages methods}.each do |type| %> diff --git a/config/routes.rb b/config/routes.rb index c11b1a1..7382a7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -125,7 +125,7 @@ Noosfero::Application.routes.draw do # cache stuff - hack match 'public/:action/:id', :controller => 'public' - match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format}/, :conditions => { :if => lambda { |env| !Domain.hosting_profile_at(env[:host]) } } + match ':profile/*page/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => /#{Noosfero.identifier_format}/, :constraints => EnvironmentDomainConstraint.new match '*page/versions', :controller => 'content_viewer', :action => 'article_versions' # match requests for profiles that don't have a custom domain diff --git a/public/javascripts/rails.js b/public/javascripts/rails.js new file mode 100644 index 0000000..309d74d --- /dev/null +++ b/public/javascripts/rails.js @@ -0,0 +1,398 @@ +(function($, undefined) { + +/** + * Unobtrusive scripting adapter for jQuery + * https://github.com/rails/jquery-ujs + * + * Requires jQuery 1.7.0 or later. + * + * Released under the MIT license + * + */ + + // Cut down on the number of issues from people inadvertently including jquery_ujs twice + // by detecting and raising an error when it happens. + if ( $.rails !== undefined ) { + $.error('jquery-ujs has already been loaded!'); + } + + // Shorthand to make it a little easier to call public rails functions from within rails.js + var rails; + var $document = $(document); + + $.rails = rails = { + // Link elements bound by jquery-ujs + linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]', + + // Button elements bound by jquery-ujs + buttonClickSelector: 'button[data-remote]', + + // Select elements bound by jquery-ujs + inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]', + + // Form elements bound by jquery-ujs + formSubmitSelector: 'form', + + // Form input elements bound by jquery-ujs + formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])', + + // Form input elements disabled during form submission + disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]', + + // Form input elements re-enabled after form submission + enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled', + + // Form required input elements + requiredInputSelector: 'input[name][required]:not([disabled]),textarea[name][required]:not([disabled])', + + // Form file input elements + fileInputSelector: 'input[type=file]', + + // Link onClick disable selector with possible reenable after remote submission + linkDisableSelector: 'a[data-disable-with]', + + // Make sure that every Ajax request sends the CSRF token + CSRFProtection: function(xhr) { + var token = $('meta[name="csrf-token"]').attr('content'); + if (token) xhr.setRequestHeader('X-CSRF-Token', token); + }, + + // making sure that all forms have actual up-to-date token(cached forms contain old one) + refreshCSRFTokens: function(){ + var csrfToken = $('meta[name=csrf-token]').attr('content'); + var csrfParam = $('meta[name=csrf-param]').attr('content'); + $('form input[name="' + csrfParam + '"]').val(csrfToken); + }, + + // Triggers an event on an element and returns false if the event result is false + fire: function(obj, name, data) { + var event = $.Event(name); + obj.trigger(event, data); + return event.result !== false; + }, + + // Default confirm dialog, may be overridden with custom confirm dialog in $.rails.confirm + confirm: function(message) { + return confirm(message); + }, + + // Default ajax function, may be overridden with custom function in $.rails.ajax + ajax: function(options) { + return $.ajax(options); + }, + + // Default way to get an element's href. May be overridden at $.rails.href. + href: function(element) { + return element.attr('href'); + }, + + // Submits "remote" forms and links with ajax + handleRemote: function(element) { + var method, url, data, elCrossDomain, crossDomain, withCredentials, dataType, options; + + if (rails.fire(element, 'ajax:before')) { + elCrossDomain = element.data('cross-domain'); + crossDomain = elCrossDomain === undefined ? null : elCrossDomain; + withCredentials = element.data('with-credentials') || null; + dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType); + + if (element.is('form')) { + method = element.attr('method'); + url = element.attr('action'); + data = element.serializeArray(); + // memoized value from clicked submit button + var button = element.data('ujs:submit-button'); + if (button) { + data.push(button); + element.data('ujs:submit-button', null); + } + } else if (element.is(rails.inputChangeSelector)) { + method = element.data('method'); + url = element.data('url'); + data = element.serialize(); + if (element.data('params')) data = data + "&" + element.data('params'); + } else if (element.is(rails.buttonClickSelector)) { + method = element.data('method') || 'get'; + url = element.data('url'); + data = element.serialize(); + if (element.data('params')) data = data + "&" + element.data('params'); + } else { + method = element.data('method'); + url = rails.href(element); + data = element.data('params') || null; + } + + options = { + type: method || 'GET', data: data, dataType: dataType, + // stopping the "ajax:beforeSend" event will cancel the ajax request + beforeSend: function(xhr, settings) { + if (settings.dataType === undefined) { + xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script); + } + return rails.fire(element, 'ajax:beforeSend', [xhr, settings]); + }, + success: function(data, status, xhr) { + element.trigger('ajax:success', [data, status, xhr]); + }, + complete: function(xhr, status) { + element.trigger('ajax:complete', [xhr, status]); + }, + error: function(xhr, status, error) { + element.trigger('ajax:error', [xhr, status, error]); + }, + crossDomain: crossDomain + }; + + // There is no withCredentials for IE6-8 when + // "Enable native XMLHTTP support" is disabled + if (withCredentials) { + options.xhrFields = { + withCredentials: withCredentials + }; + } + + // Only pass url to `ajax` options if not blank + if (url) { options.url = url; } + + var jqxhr = rails.ajax(options); + element.trigger('ajax:send', jqxhr); + return jqxhr; + } else { + return false; + } + }, + + // Handles "data-method" on links such as: + // Delete + handleMethod: function(link) { + var href = rails.href(link), + method = link.data('method'), + target = link.attr('target'), + csrfToken = $('meta[name=csrf-token]').attr('content'), + csrfParam = $('meta[name=csrf-param]').attr('content'), + form = $('
'), + metadataInput = ''; + + if (csrfParam !== undefined && csrfToken !== undefined) { + metadataInput += ''; + } + + if (target) { form.attr('target', target); } + + form.hide().append(metadataInput).appendTo('body'); + form.submit(); + }, + + /* Disables form elements: + - Caches element value in 'ujs:enable-with' data store + - Replaces element text with value of 'data-disable-with' attribute + - Sets disabled property to true + */ + disableFormElements: function(form) { + form.find(rails.disableSelector).each(function() { + var element = $(this), method = element.is('button') ? 'html' : 'val'; + element.data('ujs:enable-with', element[method]()); + element[method](element.data('disable-with')); + element.prop('disabled', true); + }); + }, + + /* Re-enables disabled form elements: + - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`) + - Sets disabled property to false + */ + enableFormElements: function(form) { + form.find(rails.enableSelector).each(function() { + var element = $(this), method = element.is('button') ? 'html' : 'val'; + if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with')); + element.prop('disabled', false); + }); + }, + + /* For 'data-confirm' attribute: + - Fires `confirm` event + - Shows the confirmation dialog + - Fires the `confirm:complete` event + + Returns `true` if no function stops the chain and user chose yes; `false` otherwise. + Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog. + Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function + return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog. + */ + allowAction: function(element) { + var message = element.data('confirm'), + answer = false, callback; + if (!message) { return true; } + + if (rails.fire(element, 'confirm')) { + answer = rails.confirm(message); + callback = rails.fire(element, 'confirm:complete', [answer]); + } + return answer && callback; + }, + + // Helper function which checks for blank inputs in a form that match the specified CSS selector + blankInputs: function(form, specifiedSelector, nonBlank) { + var inputs = $(), input, valueToCheck, + selector = specifiedSelector || 'input,textarea', + allInputs = form.find(selector); + + allInputs.each(function() { + input = $(this); + valueToCheck = input.is('input[type=checkbox],input[type=radio]') ? input.is(':checked') : input.val(); + // If nonBlank and valueToCheck are both truthy, or nonBlank and valueToCheck are both falsey + if (!valueToCheck === !nonBlank) { + + // Don't count unchecked required radio if other radio with same name is checked + if (input.is('input[type=radio]') && allInputs.filter('input[type=radio]:checked[name="' + input.attr('name') + '"]').length) { + return true; // Skip to next input + } + + inputs = inputs.add(input); + } + }); + return inputs.length ? inputs : false; + }, + + // Helper function which checks for non-blank inputs in a form that match the specified CSS selector + nonBlankInputs: function(form, specifiedSelector) { + return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank + }, + + // Helper function, needed to provide consistent behavior in IE + stopEverything: function(e) { + $(e.target).trigger('ujs:everythingStopped'); + e.stopImmediatePropagation(); + return false; + }, + + // replace element's html with the 'data-disable-with' after storing original html + // and prevent clicking on it + disableElement: function(element) { + element.data('ujs:enable-with', element.html()); // store enabled state + element.html(element.data('disable-with')); // set to disabled state + element.bind('click.railsDisable', function(e) { // prevent further clicking + return rails.stopEverything(e); + }); + }, + + // restore element to its original state which was disabled by 'disableElement' above + enableElement: function(element) { + if (element.data('ujs:enable-with') !== undefined) { + element.html(element.data('ujs:enable-with')); // set to old enabled state + element.removeData('ujs:enable-with'); // clean up cache + } + element.unbind('click.railsDisable'); // enable element + } + + }; + + if (rails.fire($document, 'rails:attachBindings')) { + + $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }}); + + $document.delegate(rails.linkDisableSelector, 'ajax:complete', function() { + rails.enableElement($(this)); + }); + + $document.delegate(rails.linkClickSelector, 'click.rails', function(e) { + var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey; + if (!rails.allowAction(link)) return rails.stopEverything(e); + + if (!metaClick && link.is(rails.linkDisableSelector)) rails.disableElement(link); + + if (link.data('remote') !== undefined) { + if (metaClick && (!method || method === 'GET') && !data) { return true; } + + var handleRemote = rails.handleRemote(link); + // response from rails.handleRemote() will either be false or a deferred object promise. + if (handleRemote === false) { + rails.enableElement(link); + } else { + handleRemote.error( function() { rails.enableElement(link); } ); + } + return false; + + } else if (link.data('method')) { + rails.handleMethod(link); + return false; + } + }); + + $document.delegate(rails.buttonClickSelector, 'click.rails', function(e) { + var button = $(this); + if (!rails.allowAction(button)) return rails.stopEverything(e); + + rails.handleRemote(button); + return false; + }); + + $document.delegate(rails.inputChangeSelector, 'change.rails', function(e) { + var link = $(this); + if (!rails.allowAction(link)) return rails.stopEverything(e); + + rails.handleRemote(link); + return false; + }); + + $document.delegate(rails.formSubmitSelector, 'submit.rails', function(e) { + var form = $(this), + remote = form.data('remote') !== undefined, + blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector), + nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector); + + if (!rails.allowAction(form)) return rails.stopEverything(e); + + // skip other logic when required values are missing or file upload is present + if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) { + return rails.stopEverything(e); + } + + if (remote) { + if (nonBlankFileInputs) { + // slight timeout so that the submit button gets properly serialized + // (make it easy for event handler to serialize form without disabled values) + setTimeout(function(){ rails.disableFormElements(form); }, 13); + var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]); + + // re-enable form elements if event bindings return false (canceling normal form submission) + if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); } + + return aborted; + } + + rails.handleRemote(form); + return false; + + } else { + // slight timeout so that the submit button gets properly serialized + setTimeout(function(){ rails.disableFormElements(form); }, 13); + } + }); + + $document.delegate(rails.formInputClickSelector, 'click.rails', function(event) { + var button = $(this); + + if (!rails.allowAction(button)) return rails.stopEverything(event); + + // register the pressed submit button + var name = button.attr('name'), + data = name ? {name:name, value:button.val()} : null; + + button.closest('form').data('ujs:submit-button', data); + }); + + $document.delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) { + if (this == event.target) rails.disableFormElements($(this)); + }); + + $document.delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) { + if (this == event.target) rails.enableFormElements($(this)); + }); + + $(function(){ + rails.refreshCSRFTokens(); + }); + } + +})( jQuery ); diff --git a/test/integration/assigning_validator_organizations_to_regions_test.rb b/test/integration/assigning_validator_organizations_to_regions_test.rb index 31a1d4c..20a8d07 100644 --- a/test/integration/assigning_validator_organizations_to_regions_test.rb +++ b/test/integration/assigning_validator_organizations_to_regions_test.rb @@ -10,8 +10,8 @@ class AssigningValidatorOrganizationsToRegionsTest < ActionController::Integrati org2 = Organization.create!(:name => 'Organization two', :identifier => 'org2') Region.destroy_all - region1 = Region.create!(:name => "Region 1", :environment_id => env.id) - region2 = Region.create!(:name => "Region 2", :environment_id => env.id) + region1 = create(Region, :name => "Region 1", :environment_id => env.id) + region2 = create(Region, :name => "Region 2", :environment_id => env.id) login('ze', 'test') diff --git a/test/integration/blocks_test.rb b/test/integration/blocks_test.rb index a04eceb..862edb1 100644 --- a/test/integration/blocks_test.rb +++ b/test/integration/blocks_test.rb @@ -46,7 +46,7 @@ class BlocksTest < ActionController::IntegrationTest block = blog_on_article_block_bootstrap p = block.owner b = block.article - f = fast_create(Folder, :name => 'Folder1', :profile_id => p.id) + f = Folder.create!(:name => 'Folder1', :profile => p) b.parent = f b.save! get "/profile/#{block.owner.identifier}" diff --git a/test/integration/categories_menu_test.rb b/test/integration/categories_menu_test.rb index cc22fb3..03c0348 100644 --- a/test/integration/categories_menu_test.rb +++ b/test/integration/categories_menu_test.rb @@ -32,8 +32,10 @@ class CategoriesMenuTest < ActionController::IntegrationTest end should 'cache the categories menu' do - ActionView::Base.any_instance.expects(:cache).with(Environment.default.id.to_s + "_categories_menu") + ActionController::Base.perform_caching = true + HomeController.any_instance.expects(:fragment_cache_key).with(Environment.default.id.to_s + "_categories_menu").returns('dir') get '/' + ActionController::Base.perform_caching = false end end diff --git a/test/integration/enable_disable_features_test.rb b/test/integration/enable_disable_features_test.rb index 3a83c78..95b86ce 100644 --- a/test/integration/enable_disable_features_test.rb +++ b/test/integration/enable_disable_features_test.rb @@ -4,7 +4,6 @@ class EnableDisableFeaturesTest < ActionController::IntegrationTest all_fixtures def test_enable_features - uses_host 'anhetegua.net' login 'ze', 'test' get '/admin/features' diff --git a/test/integration/manage_documents_test.rb b/test/integration/manage_documents_test.rb index 7331fbd..5f1b94d 100644 --- a/test/integration/manage_documents_test.rb +++ b/test/integration/manage_documents_test.rb @@ -83,7 +83,7 @@ class ManageDocumentsTest < ActionController::IntegrationTest get '/myprofile/myuser/cms' assert_response :success - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/myuser/cms/destroy/#{article.id}", :onclick => /confirm/ } + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/myuser/cms/destroy/#{article.id}", 'data-confirm' => /Are you sure/ } post "/myprofile/myuser/cms/destroy/#{article.id}" assert_response :redirect diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 6d7d29c..fb3a978 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -258,16 +258,14 @@ class RoutingTest < ActionController::IntegrationTest should 'have route to versions of an article' do - assert_routing('/ze/work/free-software/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => 'ze', :page => ['work', "free-software"]) + assert_routing('/ze/work/free-software/versions', :controller => 'content_viewer', :action => 'article_versions', :profile => 'ze', :page => 'work/free-software') end should 'have route to versions of an article if profile has domain' do user = create_user('testuser').person domain = Domain.create!(:name => 'example.com', :owner => user) - ActionController::TestRequest.any_instance.expects(:host).returns('www.example.com') - - assert_routing('/work/free-software/versions', :controller => 'content_viewer', :action => 'article_versions', :page => [ 'work', 'free-software'] ) + assert_routing('http://www.example.com/work/free-software/versions', :controller => 'content_viewer', :action => 'article_versions', :page => 'work/free-software') end diff --git a/test/integration/tiny_mce_languages_test.rb b/test/integration/tiny_mce_languages_test.rb index c2ea65a..24f00c5 100644 --- a/test/integration/tiny_mce_languages_test.rb +++ b/test/integration/tiny_mce_languages_test.rb @@ -12,7 +12,7 @@ class TinyMceLanguagesTest < ActionController::IntegrationTest def assert_exists_tinymce_language_file(file) filename = Rails.root.join("public", "javascripts", "tinymce", "jscripts", "tiny_mce", file) - assert(File.exists?(filename), filename + " must exist") + assert(File.exists?(filename), "#{filename} must exist") end -- libgit2 0.21.2