Blog

Testing Your Scrivito-Based Rails App 

posted by Antony

The question of how to test Scrivito-based apps comes up less often than you would expect, but none the less it is asked. Remember that Scrivito is just Ruby, but being mainly an API, there are some caveats. It is best kept in mind what Scrivito is, a Content Management System, so your testing will essentially revolve around views.

To start, the Scrivito SDK is fully tested.  So, as usual, there's no need to test the API.

TL;DR

For model/unit tests it is recommended to limit testing to the model attributes.

Your views and content are best tested with either JavaScript or more as a full integration test. We recommend to set up a separate CMS and test capybara style with a web-driver of some sort.

Pages and widgets themselves are tested like normal Rails models. Actually, there is no need to  test stuff coming from the CMS. We recommend to stub the CMS attribute methods and only test your custom methods as one would normally do with a standard Rails model. 

Model specs

Here is a sample model spec based on Scrivito's example application:

require 'rails_helper' RSpec.describe BlogPostPage, type: :model do it { is_expected.to respond_to(:abstract) } it { is_expected.to respond_to(:author_email) } it { is_expected.to respond_to(:author_name) } it { is_expected.to respond_to(:image) } it { is_expected.to respond_to(:published_at) } describe :text_extract do it "returns an abstract when one is provided" do short_abstract = "this is a short abstract of the blog post" blog = BlogPostPage.new allow(blog).to receive(:abstract).and_return(short_abstract) expect(blog.text_extract).to eq(short_abstract) end end end

Here is a little more complex model spec that additionally tests nested widgets:

require 'rails_helper' RSpec.describe PanelGroupWidget, type: :model do it { is_expected.to respond_to(:layout_type) } it { is_expected.to respond_to(:panels) } describe :text_extract do it "returns text from each widget" do text = "this is the text in the text widget" text_widget = TextWidget.new allow(text_widget).to receive(:text).and_return(text) widget = PanelWidget.new allow(widget).to receive(:content).and_return([text_widget]) group = PanelGroupWidget.new allow(group).to receive(:panels).and_return([widget]) expect(group.text_extract).to eq([[text]]) end it "returns deliminated text when there are multiple texts" do text1 = "this is the text in the first text widget" text2 = "two is the text in the second text widget" text_widget1, text_widget2 = TextWidget.new, TextWidget.new allow(text_widget1).to receive(:text).and_return(text1) allow(text_widget2).to receive(:text).and_return(text2) widget = PanelWidget.new allow(widget).to receive(:content).and_return([text_widget1, text_widget2]) group = PanelGroupWidget.new allow(group).to receive(:panels).and_return([widget]) expect(group.text_extract).to eq([[text1, text2]]) end end end

Controller specs

Here you can find a few example controller specs and a little spec helper for mocking pages and widgets. Scrivito provides a helper method, for_scrivito_obj, as some controllers do not have explicit routes by default. Changes in Rails 5 render the controller specs optional. These examples don't really test anything but that "it works". Nevertheless, they are included here to show some available patterns.

Example spec_helper for mocking

module ScrivitoSpecHelpers def mock_obj(klass, attributes={}) obj = klass.new attributes.each do |name, value| allow(obj).to receive(name) { value } end obj end def mock_widget(klass, attributes={}) widget = klass.new attributes.each do |name, value| allow(widget).to receive(name) { value } end widget end end

Example controller test with mocking

require 'rails_helper' RSpec.describe BlogPostPageController, type: :controller do let(:obj) { mock_obj(BlogPostPage, published_at: Time.current - 1.year) } describe 'GET index' do it 'renders the blog post page' do request.for_scrivito_obj(obj) get :index expect(response.status).to eq(200) end end end

Integration tests

For the integration tests, it would be best to mock your data requests and test only the code of your app. In an effort to keep it simple, and since one can start up the example app with consistent sample data, we are illustrating a request spec without mocking. The following example is testing that the content is rendered as expected, based on the parameters set in the controller.

Request specs

require 'rails_helper' RSpec.describe "BlogPage", type: :request do describe "GET /blog" do before do get "/blog" @posts = BlogPostPage.all.order(published_at: :desc).to_a end it "renders the blog overview page" do expect(response).to have_http_status(200) expect(response.body).to include "Tags" expect(response.body).to include "Feed" expect(response.body).to include "Photo Stream" assert_select "a[href=?]", "/#{@posts[0].title.parameterize}-#{@posts[0].id}" end it "shows titles of only the last 3 recent posts" do assert_select "div.col-md-9" do assert_select "div.row", 3 do assert_select "h4", @posts[0].title assert_select "h4", @posts[1].title assert_select "h4", @posts[2].title end end end it "does not include titles of more than the last 3 recent posts" do assert_select "h4", count: 0, html: @posts[3].title end it "includes images to the last 5 recent posts in the photo stream" do assert_select "div.col-md-3" do assert_select "div.panel" do assert_select "h2.panel-title", "Photo Stream" assert_select "ul.list-inline li a img", 5 assert_select "a[href=?]", "/#{@posts[0].title.parameterize}-#{@posts[0].id}" assert_select "a[href=?]", "/#{@posts[1].title.parameterize}-#{@posts[1].id}" assert_select "a[href=?]", "/#{@posts[2].title.parameterize}-#{@posts[2].id}" assert_select "a[href=?]", "/#{@posts[3].title.parameterize}-#{@posts[3].id}" assert_select "a[href=?]", "/#{@posts[4].title.parameterize}-#{@posts[4].id}" end end end it "has pagination" do assert_select ".previous", "← Older" expect(response.body).to_not include "Newer" end end end

This code tests the blog overview page. It checks whether it's returning the right blog posts and page elements, based on the parameters set in the controller.

If you cannot mock or do not wish to, it is recommended to set up additional Scrivito CMS instances to be used as testing environments. They would be used to create a working copy, add the content, publish and eventually delete everything with each run of the test. Keep in mind, an instance would only be able to deal with a single run at a time. So, if a developer wants to run such a test locally, they would have to have their own CMS instance. Otherwise, if there’s another developer running these tests in parallel against the same CMS instance, they will override each other’s data, resulting in non-predictable errors. Also, if there are CI machines running integration tests with publishes, each of these machines needs its own erasable CMS instance.

Creating the test tenant content is best done either in the test or via seed files. We advise to not copy anything from the production instance and in general completely isolate these two data sources in order to keep the production tenant as far from danger zones as possible. Also, in order to keep the tests as fast as possible, the seed data should to be as tiny as possible.

At Infopark, we use the following test workflow:

  • Reset a CMS instance at the very beginning of the test suite.
  • Run tests making publishes.

Note, we do not reset the tenant on each run, since it’s a relatively expensive and long-running operation.

In a nutshell …

… you have seen some ways to test your Rails app when used with Scrivito. Test your app and code as you would normally, and when testing content, test the view.

Scrivito 1.8.1 Release Notes

posted by Andreas

Bugfixes

This is a bugfix release that solves two widget-related issues, and one referring to the Content Browser.

  • Restoring a widget that originally had a direct subwidget (which was deleted) no longer causes an error.
  • When restoring a widget, attributes that were originally empty are now cleared.
  • The Content Browser now applies the “base_query” if provided.

Sorry for the inconvenience these bugs might have caused. Please update your Gemfile and run “bundle.”

Thank you for using Scrivito!

Refurbished Account Settings

We are proud to present our new account settings interface on my.scrivito.com to all our Scrivito customers!

After logging in to Scrivito, you can access your settings via the menu behind your user name at the top right.

In the new account settings section you can comfortably do the following:

  • Access your demo or reset it
  • Manage your CMSs (rename, delete, set API keys)
  • Maintain your plan and billing settings

All your CMSs are neatly listed in the “CMS Instances” section which also offers a search in case you have lots of them.

Click the title or the gear icon of an entry to change the CMS's settings, e.g. its title or API keys, or to reset it, etc. Click “Create a CMS” to add another CMS instance to your account.

We hope you find the new account settings section as useful as we do! Check it out now!

Scrivito 1.8.0 Release Notes

posted by Andreas

Improvements

Content Browser

The Content Browser is significantly faster now because it loads much more data in parallel. Just watch the main area while image thumbnails are being displayed!

Also, the multi-selection mode is now directly accessible in the user interface. It lets you select several items instead of just one.

This comes in handy if, for example, you want to delete a bunch of outdated images or the like. The multi-selection switch becomes available if the Content Browser is opened via the sidebar. Note that toggling the mode deselects all items.

More responsive changes list with better sorting

We've improved the look and feel of the changes dialog that lets you comfortably access the modified CMS objects of a working copy. Not only are the changes loaded much faster now, the not yet visible ones also show up continuously and smoothly as you scroll down.

The list of changed CMS objects can now be sorted using a drop-down menu instead of column headers. There are three sort criteria, last change date, object type name, and modification type. For each of them, both sort directions (ascending and descending) are available.

Configurable minimum request timeout

The Scrivito SDK fetches the content to display from the Scrivito backend located in European data centers. In case the content is requested from a far-off location, the response times might be longer than the SDK permits, resulting in a failure to load the data in time. To remedy this, you can now configure the minimum timeouts to apply to connections to the Scrivito API endpoint:

# config/initializers/scrivito.rb Scrivito.configure do |config| # … config.minimum_open_timeout = 1.0 config.minimum_ssl_timeout = 1.5 config.minimum_read_timeout = 1.0 # … end

These timeouts override shorter timeouts specified on a per-request basis, but not longer ones.

Other improvements

  • The Content Browser now ignores CMS objects of the MigrationStore type. Such objects are used by the SDK internally to identify migrations that have already been run.
  • The Scrivito::Workspace.use('workspace title') method now fails with a proper error message if there is no working copy with the given title. Previously, it failed with an unclear error message.

Scrivito 1.7.0 Release Notes

posted by Andreas

Improvements

Rails 5.0 support

This release of Scrivito adds support for Rails 5.0 to the SDK. Rails 5.0 was released this summer and includes numerous speed improvements, refactorings and new features (see the Rails 5.0 overview for details).

Scrivito continues to support Rails 4.2, so in case you don't want to upgrade to Rails 5.0 yet, you can still update Scrivito to the latest version. A simple bundle update scrivito does the trick. There is one small exception: We no longer support the json gem in versions prior to 1.8.3. Previously, json 1.7.7 or newer was supported.

We worked hard to support both Rails versions and to facilitate a smooth transition from Rails 4.2 to 5.0. If you want to upgrade your application to Rails 5.0, please first upgrade to Scrivito 1.7.0 or later, then update Rails itself. See the Guide for Upgrading Ruby on Rails for a detailed list of changes between Rails 4.2 and 5.0.

We continue to support Ruby 2.1. Rails 5.0, however, requires Ruby 2.2.2 or newer. Since Ruby 2.1 has already reached a state in which it only receives security fixes, it is highly advised to upgrade to a newer Ruby version (see the Support plans for Ruby 2.0.0 and 2.1 for details).

Other improvements and changes

  • Due to optimizations regarding the saving indicator, adding widgets to and removing them from pages using the UI is now performed faster.
  • Scrivito has been optimized with respect to connections to the backend, which are now established in advance. Depending on your connection quality, this can save 100 ms or more per request during UI initialization.
  • The documented signature of scrivito_cache has been corrected: scrivito_cache should not be called with options=nil.
  • Due to unexpected behavior in the “Deletions” view mode, the Content Browser is now disabled in this mode.
  • Assets are now compiled deterministically into the “scrivito_app_extensions.js” file, yielding the same JavaScript file independently of the host OS or machine.
  • We fixed a bug that caused the changes list to redirect to the wrong URL for non-binary CMS objects after removing the /:id route.
  • In rare cases, opening a widget or navigation menu caused an error. This bug has been fixed.
  • The Content Browser is now able to handle different error situations in which an image cannot be displayed. If, for example, an image cannot be transformed due to its type or if the image is too large, the Content Browser now displays an appropriate thumbnail with an explanatory message.

    100 % CDN-Backed Asset Delivery

    One of the central benefits of using Scrivito is that binary content, e.g. images, is delivered fast and reliably to the visitors of your website, no matter where they're located. The technology behind this, the transparent use of a Content Distribution Network, isn't new to our CMS. In fact, CDN integration was an early milestone in the course of developing Scrivito.

    So what's the news?

    As of mid-December 2016, all content delivery is backed by the CDN behind Scrivito, Amazon CloudFront. For this to work, we recently changed the asset URLs.

    Before that, only assets part of the published content and of working copies being previewed were delivered via the CDN. In editing mode, assets were served solely by EU-based Amazon S3.

    So, when editing pages containing any amount of images, pages should load speedier now, especially if you're not located in the EU.

    Happy editing!

    Scrivito 1.5.5 and 1.6.1 Release Notes

    posted by Dave

    Bugfixes

    In some cases, the referencelist attribute editor was unable to process references with purely numeric IDs.

    According to our in-house mathematician, Scrivito users had a 0,054% chance to hit this issue. The number increased with the size of the referencelist. After fixing the issue, the chance of a clash is most probably zero.

    Please update your Gemfile and run “bundle.”

    Thank you for using Scrivito!

    Scrivito 1.4.3 and 1.5.4 Release Notes

    posted by Andreas

    Bugfixes

    These releases make Scrivito compatible with jquery-ui-rails 6.

    On November 29, 2016, jquery-ui-rails 6 has been released. Unfortunately, Scrivito was incompatible with this new major version.

    Sorry to everyone who has been bitten by this. Please update your Gemfile and run “bundle.”

    Thank you for using Scrivito!

    Preview URL change on Thursday, 2016-12-08

    posted by Tilo Prütz

    We're going to change our preview URL for assets from *.amazonaws.com to preview-cdn.scrvt.com on Thursday, 2016-12-08 around 11:00 CET.

    Existing links will continue to work, meaning that the legacy URLs pointing to *.amazonaws.com won't expire prematurely.

    The change will be done automatically in our backend. No SDK upgrade is necessary.

    If you are using your own CDN and have hardcoded the old preview URL in the Scrivito::BinaryRewrite callback, you might need to adapt it.

    Scrivito 1.6.0 Release Notes

    posted by Andreas

    Improvements

    Introducing the sidebar

    Over the past months, as we added notification icons and the display size switch to the Scrivito panel, we realized that the panel has become too small to nicely accommodate any further controls. So we complemented it with a sidebar for better accessibility and unique presentation of the available functions and pieces of information.

    The working copy menu as well as all the buttons and notification icons in the top right corner of the Scrivito panel, except the page menu, have moved to the sidebar. (The page menu icon has been redesigned, too, by the way.)

    To give you full control over the amount of space available for editing content, the sidebar can be minimized, left open, or hidden together with the Scrivito top panel. In the minimized state, there are four main buttons each of which represents a distinct set of functions: the Content Browser, working copies, notifications, and display sizes. The Content Browser is opened directly, but clicking one of the other buttons opens the sidebar, revealing the functions included in the corresponding set. Clicking the button once more minimizes the sidebar again.

    The sidebar is automatically opened and closed as the mouse pointer enters or, respectively, leaves it unless the button of the currently open section is clicked to close it. In this case, hovering temporarily has no effect.

    At the bottom right, there is a button for hiding the sidebar as well as the top panel. In this state, the only visible element is this button which, however, looks slightly different. Click it to have the panel and the sidebar displayed again.

    We also redesigned the mode switch to make the selected mode better distinguishable from the other ones.

    In the “Changes” position, the switch now has a meaningful background color reflecting the selected “Changes” mode: green for “Additions”, red for “Deletions”, and blue for “All changes”.

    We hope you like the new sidebar as much as we do!

    Better confirmation prompt on deletion

    When deleting a CMS object, and this object is referenced by other objects via a link, linklist, reference, or referencelist attribute, or via an <a href> or an <img src> tag inside an html attribute, Scrivito now lets you know this, and also which objects contain these links.

    If you're unsure about a CMS object referencing the one to delete, just click the given description to open this referencing object in the Content Browser.

    The dialog uses the description_for_editor callback of the model classes of the referencing objects, making it possible to have the page titles displayed, for example.

    Improved reference attribute editor

    Users can now clear single references using a delete button.

    To make this button appear in a reference editor, specify the data-scrivito-editors-allow-delete=true option in the call to scrivito_tag:

    <%= scrivito_tag(:div, widget, :my_reference, data: { scrivito_editors_allow_delete: true }) %>

    Introducing numeric attribute types

    Scrivito has two new attribute types, integer and float, that make it easier to handle values of these types. You no longer need to use strings to represent them, validate these strings and convert them to a numeric type. Also, sorting objects by true numbers yields properly sorted lists as opposed to sorting them by numbers represented as strings.

    Usage

    First, add the desired attributes to the model class concerned:

    class SomePage < Obj attribute :float_attribute, :float attribute :integer_attribute, :integer end

    To have the attribute values displayed and make them editable in a details view, render them as shown below.

    <%= scrivito_details_for("Float attribute") do %> <%= scrivito_tag(:div, obj, :float_attribute) %> <% end %> <%= scrivito_details_for("Integer attribute") do %> <%= scrivito_tag(:div, obj, :integer_attribute) %> <% end %>

    For both types, the provided number editor supports a range and a step size you can specify using the min, max, and step options:

    <%= scrivito_details_for("Float attribute") do %> <%= scrivito_tag(:div, obj, :float_attribute, {data: { scrivito_editors_min: 0, scrivito_editors_max: 10, scrivito_editors_step: 0.1 } }) %> <% end %>

    Syntactically invalid values (e.g. “2oo”) and values not in the given range are not stored and marked as an error.

    Updated slider editor

    The slider editor now supports these new attribute types. Here is an example of a slider for durations ranging from 1 to 10 seconds, in steps of 0.5 seconds:

    scrivito_tag(@obj, :float_duration_seconds, { data_scrivito_editors_min: 1, data_scrivito_editors_max: 10, data_scrivito_editors_step: 0.5, }, editor: :slider)

    Semi-automatic conversion of numbers stored as strings

    If you have been using string attributes for representing numbers, you can simply change the type of these attributes to integer or float in the model classes without having to migrate the attribute values, i.e. change their format. When retrieving strings as values of integer or float attributes, Scrivito automatically converts them to the corresponding type.

    However, converted integer and float attribute values are not automatically written back to the attributes, causing them not to be considered in searches.

    CMS objects now have a backlinks method (via their base class, BasicObj). backlinks searches for CMS objects containing one or more attributes linking to a given CMS object. So the search returns the CMS objects in which at least one html, link, linklist, reference or referencelist attribute links to the CMS object.

    For rendering such backlinks on details views, Scrivito includes a new helper, scrivito_backlinks:

    <%= scrivito_details_for "What links here?" do %> <%= scrivito_backlinks @obj %> <% end %>

    The helper generates HTML markup that fits in with the general look and feel of Scrivito's user interface.

    Establishing CMS object associations

    The Scrivito SDK now includes helpers for establishing CMS object associations in the style of Active Record Associations. Three new methods, belongs_to, belongs_to_many and has_many, allow you to define one-to-one and one-to-many associations between CMS objects. See Modeling Object Associations for examples and further details.

    Changelog

    The changelog reflects minor improvements and bugfixes that were made in addition to the changes listed above.