Slide 1

Slide 1 text

DRUPAL CONTINUOUS INTEGRATION Introduction Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 2

Slide 2 text

Continuous Integration “Continuous Integration” is a software development practice where members of a team integrate work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including tests) to detect integration errors as quickly as possible. — Martin Fowler Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 3

Slide 3 text

Continuous Integration • automating repetitive tasks such as build, QA and deploy • fast and frequent integrations • fast feedback loop, catch errors asap • ability to deploy quickly and keep track of every released set of code ADVANTAGES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014-2015 http://tenwarp.com/

Slide 4

Slide 4 text

Continuous Integration developer commits triggers config static analysis unit / functional tests runs runs runs report FAIL deploy smoke / functional tests report SUCCESS SUCCESS FAIL CI server revision control log logs logs logs BUILD PROCESS EXAMPLE Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 5

Slide 5 text

Continuous Integration TOOLS • SCM: git, svn, cvs, … • CI SERVER: jenkins • BUILD TOOL: phing • DRUPAL AUTOMATION: drush • DEPENDENCIES MANAGEMENT: drush make & composer • DRUPAL CDD TOOLS: features & migrate • TESTING: PHPUnit & Behat Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 6

Slide 6 text

Continuous Integration DRUPAL - I Drupal, as opposed to “classic” frameworks such as Symfony or Zend, does not make the setup and maintenance a Continuous Integration cycle easy due to the high coupling between data and configuration within its database. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 7

Slide 7 text

Continuous Integration DRUPAL - II To overcome this problem it’s necessary to adopt a strictly controlled, disciplined work methodology and to make use of a set of tools that allow us to transform configuration into code, to easily deal with content and to incrementally update database’s schema. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 8

Slide 8 text

Continuous Integration WORKFLOW - I A developer’s workflow consist not only in the plain old features implementation, but also in the correct, precise and complete export of all the element of configuration that are necessary to rebuild the functionalities on several environments. ! Key modules: Features and Strongarm. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 9

Slide 9 text

Continuous Integration WORKFLOW - II It’s often necessary to setup some default content that may vary during development and that might be expanded or updated once the site has been published. It’s therefore necessary to formalise in code all the data import procedures in order to better administrate them and to be able to replicate them at any given time. ! Key module: Migrate. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 10

Slide 10 text

Continuous Integration WORKFLOW - III As soon as a self-contained task such a bug-fix or a new feature implementation has been completed, it’s necessary to commit and push that unit of work to the central repository in order to integrate, test and deploy it automatically on the development environment. ! Key tools: git/svn. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 11

Slide 11 text

Continuous Integration WORKFLOW - IV ! Writing automated tests is necessary in order to guarantee the behaviour of a given implementation and to make sure that future changes won’t break the work already done, creating regressions and dealing potential damage to users and clients. ! Key tools: PHPUnit, Behat. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 12

Slide 12 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 13

Slide 13 text

Continuous Integration EXERCISES 1. What are the main advantages of a Continuous Integration system? 2. When do Unit Tests are executed during the build? How about functional? Why are they executed at different times? 3. Why is it important to write tests? 4. Which benefits come from using a Source Control Management system? 5. Why is Drupal so hard -compared to other tools such as “classic” frameworks- to use in a Continuous Integration environment? 6. What tools allow Drupal to be successfully employed in a Continuous Integration development cycle and for what reasons? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 14

Slide 14 text

DRUPAL CONTINUOUS INTEGRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 15

Slide 15 text

Phing is a build system written in PHP that allows developers to describe the automated operations used during development, deployment and maintenance using plain XML files. Phing Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 16

Slide 16 text

• Simplicity: it’s based on well-known, declarative XML syntax and features a very rich documentation • Power: thanks to the big amount of available tasks, it is able to perform almost anything you could do using a command line interpreter • Extensibility: as it is written in PHP, it’s easy to extend and debug • Automation: the whole application configuration and deployment process can be handled by build files Phing BENEFITS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 17

Slide 17 text

• execute tests and log their results • perform filesystem operations • interact with source configuration management systems • generate documentation • transfer files from/to remote systems • fire drush commands • invoke shell commands Phing FEATURES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 18

Slide 18 text

There are two available file types in Phing to configure the build: .properties and .xml files. The former contains a list of key/value variables, while the latter contains the “instructions” to execute. In a usual setup there are up to five different environments: loc, dev, stage, debug and prod. Each of these ones will be configured using a dedicated .properties file in order to make the build file environment-agnostic so to reuse most of its code. Phing MAIN FILES AND ENVIRONMENTS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 19

Slide 19 text

Phing PROPERTY FILE Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 20

Slide 20 text

Phing BUILD FILE - PROJECT TAG For every build file, there must be a single, unique, root “project" tag. Within that "target", "task" and “type" tags can be used. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 21

Slide 21 text

Phing BUILD FILE - TYPE TAG Besides default types such as numbers or strings, it is possible to define more complex, nested types. An interesting feature of Phing’s types is the ability to be referenced by id later. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 22

Slide 22 text

Phing BUILD FILE - TARGET TAG “Targets” are collections of components that are assigned a unique name within the project. It is possible to invoke other targets within a target and it’s also possible to set dependencies between different targets. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 23

Slide 23 text

Phing BUILD FILE - TASK TAG “Task”s are responsible for the execution of specific units of work and are usually configurable through attributes and nested tags. A few examples of what tasks do could be: files copying, archive compression or drush commands execution. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 24

Slide 24 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 25

Slide 25 text

Phing EXERCISES 1. What are the main benefits of using a build system? 2. Why is Phing more convenient than a bunch of shell scripts? 3. What files does Phing use to work? What’s their use? 4. What are the fundamental elements of a build file? What’s their use? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 26

Slide 26 text

DRUPAL CONTINUOUS INTEGRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 27

Slide 27 text

Drush Drush is a command-line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those who spend their working hours hacking away at the command prompt. — Drush Team Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 28

Slide 28 text

Drush • Drupal Power: ability to simply, swiftly and programmatically execute Drupal-specific tasks • Integration: working with other command-line tools is very easy and effective BENEFITS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 29

Slide 29 text

Drush • code and database backup, restore e sync • cron tasks execution • automatic installations • variables, cache and logs CRUD operations • modules and themes administration • SQL commands execution • users administration • dependency management • remote tasks execution FEATURES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 30

Slide 30 text

Drush Drush make is a Drush extension that deals with modules, themes, libraries and patches management. It makes use of a manifest file to index and download the required versions of the packages used to build a Drupal website. MAKE - I Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 31

Slide 31 text

Drush MAKE - II Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 32

Slide 32 text

Drush Drush Make allows developers to specify a destination sub-folder using the “subdir” option. We’ll leverage that to establish the convention of having four sub-folders for modules: contrib, custom, devel and features. This division will help in keeping a cleaner division between the modules and will save us the burden of versioning “contrib” modules. MAKE - III Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 33

Slide 33 text

Drush Drush allows the developers to create a kind of labels called Site Aliases: these are nothing but configuration groups that fall under an arbitrary name and make the execution of some Drush commands easier. In our case those aliases match the environments defined for our project, and will look something like myproject.loc and myproject.dev. SITE ALIAS - I Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 34

Slide 34 text

Drush SITE ALIAS - II Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 35

Slide 35 text

Drush Drush looks for Aliases definitions in several locations: 1. in the path set in the “alias path” variable of the options array found in the drushrc.php file 2. in the path specified in the —alias-path command-line option 3. in one of the following folders: “/etc/drush”, “$HOME/.drush” or the current site’s “sites/all/drush” In order to keep Drush Aliases clean and separated, we’ll use our projects’ “sites/all/drush” as the go-to location to store their configuration so not to pollute the global configuration. SITE ALIAS - III Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 36

Slide 36 text

Drush Drush implements two extremely useful commands to keep files and databases in sync between hosts: rsync e sql-sync. • drush rsync @mysite.loc @mysite.dev • drush sql-sync @mysite.loc @mysite.dev SYNCHRONISATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 37

Slide 37 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 38

Slide 38 text

Drush EXERCISES 1. What’s Drush? 2. Why is it convenient to use it? 3. What are its most important features? 4. What’s the use for a make file? 5. What’s a Drush Site Alias? What’s its use? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 39

Slide 39 text

DRUPAL CONTINUOUS INTEGRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 40

Slide 40 text

Jenkins Jenkins is a Java application useful to manage the execution of those repetitive tasks that are needed to build a software project. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 41

Slide 41 text

Jenkins • automation of deployments procedures • speed-up of repetitive tasks such as test runs or the creation of build artifacts to deploy on our projects’ target environments • logging and monitoring for a handful of code-related stats such as test results or code quality analysis • convenient web ui to configure all the needed jobs BENEFITS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 42

Slide 42 text

Jenkins FEATURES • integration with several source configuration management systems • tests results and code quality reports generation • integration with several build systems • jobs status notifications • configurable web ui • hundreds of plugins Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 43

Slide 43 text

Jenkins INSTALLATION

Slide 44

Slide 44 text

Jenkins PLUGINS

Slide 45

Slide 45 text

Jenkins JOB CREATION - I Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 46

Slide 46 text

Jenkins JOB CREATION - II Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 47

Slide 47 text

Jenkins DASHBOARD Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 48

Slide 48 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 49

Slide 49 text

Jenkins ESERCIZI 1. What’s Jenkins? 2. What are its tasks? 3. What’s a Job? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 50

Slide 50 text

DRUPAL CONTINUOUS INTEGRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 51

Slide 51 text

Features The Feature module is able to export parts of the configuration found in the database into “ordinary” Drupal modules. The ability to produce a set of files containing a specific functionality starting from the database makes them much easier to manage and deploy across several systems and installations. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 52

Slide 52 text

• Removes the need for manual, repetitive work that’s necessary to reproduce all of a website’s features and traits by encapsulating them in portable, standard Drupal modules; • Exports directly to Drupal modules or, in other words, text files: that means it’s easy to version them using a source control management system, which in turn makes easy to keep the history of changes and allows multiple people to work on the project concurrently; • Allows to transport functionalities across websites in an easy, reproducible and automatic fashion. Features BENEFITS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 53

Slide 53 text

Features Local Stage Dev Prod Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 54

Slide 54 text

Features CREATION - I Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 55

Slide 55 text

Features CREATION - II Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 56

Slide 56 text

Features ADMINISTRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 57

Slide 57 text

Features OPERATIONS - ENABLING A FEATURE Enabling a Features-exported module executes the same operations a “normal” module executes, such as: • Schema installation; • System Register and Cache update; • Execution of hook_install(); • Execution of hook_enable(). Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 58

Slide 58 text

Features OPERATIONS - UPDATE & REVERT Features Update: updating a feature consists in the act of saving some of the information found in the database into a specific Feature Module. Features Revert: reverting a feature consists in the act of writing the information found in a Feature Module into the database. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 59

Slide 59 text

Features features-components (fc) List features components. features-diff (fd) Show the difference between the default and overridden state of a feature. features-export (fe) Export a feature from your site into a module. features-list (fl) List all the available features for your site. features-revert (fr) Revert a feature module on your site. features-revert-all (fra) Revert all enabled feature module on your site. features-update (fu) Update a feature module on your site. features-update-all (fua) Update all feature modules on your site. DRUSH COMMANDS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 60

Slide 60 text

Features • Thanks to the power and flexibility granted by the Entity API it’s possible to easily create exportable Entities that are automatically picked up and handled by Features; • A lot of contrib modules are already using such possibility in order to provide exportable configuration. Some examples could be the Rules, Views or Context modules. BEYOND THE DEFAULT OPTIONS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 61

Slide 61 text

Features • Create and maintain “vertical” features such as blog, forum and front page. Avoid creating Feature Modules that group “exportables” by type, such as “all site’s views” or “all site’s rules”; • Do always use English and specific namespaces when naming components (field_blog_topics instead of field_argomenti); • Do not use abbreviations unless it’s strictly ncssry. • Keep a standard format when naming fields: try not to mix casing, dashing, underscores and so on; • Define conventions. The Kit Project, although abandoned, is a good source of inspiration; • Strive to keep things clean and in order: try to always use Features for importing and exporting all the developed functionalities. BEST PRACTICES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 62

Slide 62 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 63

Slide 63 text

Features EXERCISES 1. What are the main goals of the Features module? 2. What components can it act upon? 3. What does feature-revert do? 4. What benefits does Features bring? 5. What’s Strongarm used for? 6. Can you name a few modules that allow their components to be exported by Features? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 64

Slide 64 text

DRUPAL CONTINUOUS INTEGRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 65

Slide 65 text

Migrate The Migrate module makes a powerful and flexible object oriented framework for importing contents into Drupal available to developers: it offers out-of- the-box support for a handful of sources, such as databases and file formats, and destinations, such as nodes and taxonomy terms. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 66

Slide 66 text

Migrate At macro-level, there’s four interesting areas to consider when working with Migrate: • An API for defining and managing the migration process; • A set of Drush commands for managing the migrations, such as import, rollback and status; • An Admin UI featuring some of the features exposed by Drush; • Some example modules that demonstrate a handful of techniques used to define custom migrations. FEATURES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 67

Slide 67 text

Migrate MigrateSource id userName email address MigrateMap sourceId destinationId MigrateDestination uid name mail UnmigratedDestination address ARCHITECTURE Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 68

Slide 68 text

Migrate SOURCE • Inherits from the abstract class MigrateSource • Provides an interface to the data set • Iterates on the source’s rows • Provides a list of fields Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 69

Slide 69 text

Migrate DESTINATION • Inherits from the abstract class MigrateDestination (and usually from the more specific MigrateDestinationEntity) • Responsible for persisting data in a specific Drupal’s content type such as user, node, comment, file. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 70

Slide 70 text

Migrate MAPPING TABLE • Inherits from MigrateSQLMap, which in turn inherits from the abstract class MigrateMap • Describes the key formats for the source and the destination • Stores all the information that’s used to import and rollback each and every single record Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 71

Slide 71 text

Migrate FIELDS MAPPING • Allows to link each destination field with a source field • Allows to transform data during the migration to fix or adapt to the destination requirements. • Allows to exclude some fields from the migration Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 72

Slide 72 text

Migrate Handlers are classes that allow to execute additional operations during the import process. They are usually used to support migrations where some specific, non-standard data is connected to Core Entities: most of the time this will be related to custom field types. HANDLER Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 73

Slide 73 text

Migrate There’s two kinds of Handler: Destination and Field. The former is used to manipulate the Destination Entity around saving (pre- and post- save) and to declare additional fields exposed by the Destination Entity. The latter works much like the Destination Handler, except its methods are invoked for each Field (as opposed to each Entity). HANDLER Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 74

Slide 74 text

Migrate Sometimes there are pieces of content that hold circular references to each other: for instance a tutorial post “part 1” could contain a reference to the “part 2” and vice-versa. STUB NODE nid 1 title tutorial part I body … series_nids 2,3,4 NODE nid 2 title tutorial part II body … series_nids 1,3,4 Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 75

Slide 75 text

Migrate During the import process the first node won’t be able to correctly link to the second as the (new) id is still unknown: Stubs solve this problem by creating and linking a temporary, empty node that only holds an id. As soon as the import process gets to the stubbed node, it will populate it with all the due information. STUB Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 76

Slide 76 text

Migrate Most of the time migrations are defined as static as that source and the destination match one-to-one: one row, one migration. However, sometimes it’s necessary to “join” multiple sources having the same structure into a single destination: this kind of migration is defined as dynamic. An example could be a migration where you need to import users from several Drupal sites into a new one. DYNAMIC MIGRATIONS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 77

Slide 77 text

Migrate When using this functionality, Drupal will take care of instantiating each migration multiple times. Besides that, a dynamic migration differs from a static migration in the following ways: • its constructors accepts an array of arguments, where each of its keys identifies an instance of the migration class; • it overrides the generateMachineName() method in order to generate a distinct machine name for each instance, so to be able to identify them. DYNAMIC MIGRATIONS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 78

Slide 78 text

Migrate migrate-analyze (maz) Analyze the source fields for a migration. migrate-audit (ma) View information on problems in a migration. migrate-auto-register (mar) Register any newly-defined migration classes. migrate-deregister Remove all tracking of a migration. migrate-fields-destination (mfd) List the fields available for mapping in a destination. migrate-fields-source (mfs) List the fields available for mapping from a source. migrate-import (mi) Perform one or more migration processes. migrate-mappings View information on all field mappings in a migration. migrate-messages (mmsg) View any messages associated with a migration. migrate-reset-status (mrs) Reset a active migration's status to idle. migrate-rollback (mr) Roll back the destination objects from a given migration. migrate-status (ms) List all migrations with current status. migrate-stop (mst) Stop an active migration operation. migrate-wipe (mw) Delete all nodes from specified content types. DRUSH COMMANDS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 79

Slide 79 text

Migrate ADMINISTRATION http://tenwarp.com/

Slide 80

Slide 80 text

Migrate • Migrate D2D - set of utilities for migrating content from other versions of Drupal (5, 6, 7) to Drupal 7; • Migrate Extras - integrates several contrib modules such as Rules or Pathauto; • Commerce Migrate - Integrates Drupal Commerce’s Entities; • Wordpress/Typo3/PHPBB Migrate - set of utilities to aid the import of content from other well-known platforms. BEYOND THE DEFAULT OPTIONS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 81

Slide 81 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 82

Slide 82 text

Migrate EXERCISES 1. What’s the main goal of the Migrate module? 2. What could be the sources for a migration? 3. What’s the use for the mapping table? 4. What does migrate-rollback do? 5. When is appropriate to use Migrate? 6. What’s a dynamic migration? How is it used? 7. What’s a stub? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 83

Slide 83 text

DRUPAL CONTINUOUS INTEGRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 84

Slide 84 text

Behat Behat is a testing framework written in PHP with the purpose of aiding the writing of Acceptance Tests using Behaviour Driven Development practices. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 85

Slide 85 text

Behat IT’S NOT A FUNCTIONAL TESTING FRAMEWORK It’s a tool to verify that a given system satisfies the business needs of the client. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 86

Slide 86 text

Behat Tests are written using a semi-formal language called Gherkin. In a nutshell, Behat maps Gherkin sentences -usually written in English, but it could be any language- to a class method using to regular expressions. GHERKIN Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 87

Slide 87 text

Behat GHERKIN FEATURE Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 88

Slide 88 text

Behat StoryBDD Behaviour Driven Development based on User Stories -aka StoryBDD- is a practice that helps into bringing a development team’s understanding of business requirements to the same level of the client’s. It does so by setting a formal process for asking questions about requirements and then expressing the outcome in a specific story-format. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 89

Slide 89 text

Behat For each new feature, a developer should answer the following business questions: • Who will use it? • What’s its purpose? • How is that achieved? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/ StoryBDD

Slide 90

Slide 90 text

Behat “I want users to access the site through a dedicated form” • Who will use it? a user an anonymous visitor • What’s the purpose? it’s not stated • How is that achieved? by using the login form Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/ StoryBDD

Slide 91

Slide 91 text

“I want an anonymous visitor to be able to access the site using the login form in order to access her dashboard containing her aggregated data” ! In order to see aggregated data on my dashboard as a site visitor I need to be able to login on the site Behat benefit role condition Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/ StoryBDD

Slide 92

Slide 92 text

Once a feature is defined, you should start writing all the scenarios that describe it, using the Context- Action-Outcome pattern: ! Given I am in on the homepage When I login Then I should see “Welcome Back!” And I should be on my dashboard Behat SCENARIOS context action outcome Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 93

Slide 93 text

Behat Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 94

Slide 94 text

Behat BEST PRACTICES And I should see “message” inside “.notifications > p:first-child” ! It’s recommended to avoid technical details in your scenarios as they increase the coupling between tests and code and make the stories harder for non-technical readers -such as the client- to understand. ! And I should see “message” inside notification area Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 95

Slide 95 text

Behat MINK Each test gets executed within a context where the methods invoked by the expressions used in the scenarios can access to a browser-abstraction API called Mink. There are several drivers available for Mink such as Goutte, a headless browser written in PHP that’s able to parse HTML and XML and Selenium, a full-fledged framework able to programmatically control real browsers. Oh and there’s a Drush Driver, too! :) Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 96

Slide 96 text

Behat MINK CONTEXT Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 97

Slide 97 text

Behat MINK BEST PRACTICES The Page object pattern is a way of keeping context files clean by separating UI knowledge from the actions and assertions. In other words, page objects hide the UI and expose clean services that can be used in the context classes: such abstraction makes context classes much more clear and helps avoiding duplication and improving maintainability. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 98

Slide 98 text

Behat MINK DRUSH DRIVER Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 99

Slide 99 text

Behat EXECUTION EXAMPLE Gherkin Feature Behat Mink Goutte Selenium Firefox Test Results Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 100

Slide 100 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 101

Slide 101 text

Behat EXERCISES 1. What’s Behat and what’s its main goal? 2. What’s StoryBDD? 3. What’s a scenario? 4. What languages can be used to write the tests? 5. What questions should be answered by a feature description? 6. What are Mink’s main tasks? 7. What browsers can be used run the tests? What are the main differences between them? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 102

Slide 102 text

DRUPAL CONTINUOUS INTEGRATION Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 103

Slide 103 text

PHPUnit PHPUnit is a unit testing framework, it’s part of xUnit family and it has become the de-facto PHP standard over the years. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 104

Slide 104 text

PHPUnit In software engineering, unit testing is a method by which individual units of code are tested to determine if they are fit for use. A unit is defined as the smallest part of an application that can be tested in isolation: most of the time that unit is a class or a method. UNIT TESTING Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 105

Slide 105 text

PHPUnit A SUT -an acronym for System Under Test- is defined as that subset of the software being developed that’s exercised by a test. It can be a specific method, and object or even a set of objects. DEFINITIONS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 106

Slide 106 text

PHPUnit Writing automated tests (not necessarily unit tests only) sets some goals: • Improving the quality of the product and its code; • Increasing developers’s understanding of the SUT; • Decreasing the damage caused by unexpected bugs; • Simplifying the creation, maintenance and execution of the tests; • Documenting the behaviours of the applications’ components. GOALS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 107

Slide 107 text

PHPUnit TYPES OF TESTS Unit Integration Functional Functional: they operate on the system from the outside, often by simulating/employing a browser. Integration: they check how multiple components of the system cooperate when exercised together. Unit: they make sure that the single units of code behave as expects when exercised in isolation. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 108

Slide 108 text

PHPUnit Should I test before or after development? Traditionally tests -being them unitary or functional- have always been executed once the implementation was completed. Such approach has been upturned by the rise of agile methodologies, where the emphasis is put on the ability to respond to change quickly rather than sticking to a plan. Writing tests first makes code inherently testable and therefore safer to modify and usually produces less verbose and over- engineered implementations. METHODOLOGIES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 109

Slide 109 text

PHPUnit Should I test outside-in or inside-out? As soon as development begins, a very important decision has to be made: should it start from the big picture and “zoom in” or from one or some of the details and “zoom out” in order to compose the macro-functionality at the end? METHODOLOGIES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 110

Slide 110 text

PHPUnit Should I test outside-in or inside-out? By starting development(and testing) from the outside, the developer forces himself to think as a client first, a method that helps in making requirements and (temporarily missing) dependencies more explicit. Developing “from the inside” on the other hand frees the developer from the problem of not existing dependencies at the cost of guessing what’s going to be the right set of things to implement before reaching the requested level of functionality. METHODOLOGIES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 111

Slide 111 text

PHPUnit Testing status or behaviour? There are two main ways to verify the correctness of a SUT: • Testing the state: it’s often sufficient to verify that given an initial state, once the system is exercised the ending state then matches the expected one. • Testing the behaviour: it’s sometimes necessary to look inside the black box of the SUT in order to check how the system works during the execution of the tests to make sure all the behavioural expectations were satisfied. METHODOLOGIES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 112

Slide 112 text

PHPUnit TOOLS PHPUnit provides a number of tools and helpers to aid testing, such as: • Assertions • Fixtures • Database testing helpers • Stub and mock objects • Code coverage analysis tools • Several logging formats • Integration with several functional testing tools such as Selenium Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 113

Slide 113 text

PHPUnit EXAMPLE Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 114

Slide 114 text

PHPUnit PHPUnit features about 80 assertions (of which 40 are “straight” and 40 are “negated”). It’s up to the developer to pick the best one for the test in order to have the most expressive tests and the best error messages possible. ASSERTIONS Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 115

Slide 115 text

PHPUnit FIXTURES When writing tests, one of the most tedious and time consuming parts is the preparation of the environment status before the actual execution happens: such status is known as the fixture of the test . PHPUnit features quite a big set of methods used to aid in the creation and sharing of these states, such as: setUpBeforeClass(), setUp(), assertPreConditions(), assertPostConditions(), tearDown(), tearDownAfterClass() and onNotSuccessfulTest(Exception $e). Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 116

Slide 116 text

PHPUnit DATA PROVIDERS It can be useful to be able to run multiple iterations of the same test on a larger-than-one data set. Data Providers allows for a convenient way to do just that. Such providers are nothing but a method that returns an array containing all the combinations of arguments a tests will be called with. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 117

Slide 117 text

PHPUnit TEST DOUBLES It could be helpful -or necessary- to replace a SUT’s dependencies with so-called “test doubles”, a kind of objects that looks like the dependency but it’s created with the sole purpose of satisfying testing needs. Such a replacement allows to isolate the SUT and manipulate its inputs as needed, making the test more effective and focused on the unit alone. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 118

Slide 118 text

PHPUnit TEST DOUBLES There are various kinds of Test Doubles, each serving a specific purpose. Meszaros and Fowler suggested the following five definitions: • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists; • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an InMemoryTestDatabase is a good example); Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 119

Slide 119 text

PHPUnit TEST DOUBLES • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test; • Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent; • Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 120

Slide 120 text

PHPUnit TEST DOUBLES Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 121

Slide 121 text

PHPUnit OPTIONS PHPUnit features a long list of options to configure its behaviour that’s accessible both from command line and from a configuration file. The former is a convenient way to quickly get started and try thing out, but on the long run the phpunit.xml file is probably a more convenient choice to configure the tool. It’s a good practice to commit a phpunit.xml.dist file and (git) ignore the phpunit.xml one: by doing such thing the other developers will be able to create a phpunit.xml file tailored to their own needs. PHPUnit will automatically use phpunit.xml if present or phpunit.xml.dist if not. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 122

Slide 122 text

PHPUnit OPTIONS BEST PRACTICES It’s a good practice to commit a phpunit.xml.dist file and (git) ignore the phpunit.xml one: by doing such thing the other developers will be able to create a phpunit.xml file tailored to their own needs, leaving the phpunit.xml.dist targeted at automatic builds, fore example. PHPUnit is smart enough to automatically use phpunit.xml if present or phpunit.xml.dist if not. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 123

Slide 123 text

PHPUnit OPTIONS

Slide 124

Slide 124 text

PHPUnit BEYOND THE DEFAULT OPTIONS There are more tools provided by the community than what’s implemented by PHPUnit alone. Here’s a short list of some relevant ones: • Faker - a library that generates fake-yet-plausible data for a handful of typical use cases such as names, addresses, numbers, dates, images, etc… • Mockery - a powerful mocks creation library; • Prophecy - a new, strongly-opinionated mocks creation library; • PHP-VCR - a library to record and replay HTTP calls in tests; • TuTu - a simple and flexible HTTP mocking tool. Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 125

Slide 125 text

Q & A Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/

Slide 126

Slide 126 text

PHPUnit EXERCISES 1. What’s Unit Testing? 2. What are the main types of tests? 3. What are the benefits of writing the tests before the code? 4. What’s an assertion? 5. What’s a fixture? 6. What’s a data provider? 7. What’s the difference between a Stub and a Mock? 8. What’s the purpose of the phpunit.xml file? Drupal Continuous Integration - By Claudio Beatrice - Copyright 2014 http://tenwarp.com/