Hot answers tagged

12

Since Mark's blog is offline, I'll mention some of the tools his team has implemented: Functional testing: Selenium Unit testing: Simpletest Build server: Jenkins Performance benchmarking: XDebug + Cachegrind In the two years since I asked this question I've seen some additional tools gain popularity to the TDD scene. Nowadays when you speak of Test Driven ...


11

The Maillog module is for exactly this purpose, as Maillog provides an easy possibility to log all Mails for debugging purposes. It's possible to prevent the mails to being sent, so there is no need for an extra mail server to test the mail functionality of other modules or the drupal core.


11

Parameter replacement only works for WHERE clauses, it won't work for the kind of query you're trying to run. You need to insert $start_value directly into the query string. If that value comes from an un-trustworthy source then obviously you need to sanitise it and make sure it falls within the correct bounds yourself: $start_value = (int)$start_value; // ...


11

Here's some slides on automated security reviews using Drupal tools Here are some manual steps for a review You can try to improve password strength And to make all passwords stronger, Two Factor Authentication is helpful Security Review is a good call As is Paranoia And Cracking Drupal is a good resource (disclosure, I wrote it)


11

You can get PHPUnit tests to run by bootstrapping Drupal within each of your test files. It's not ideal, but it does work. define('DRUPAL_ROOT', 'your/path/to/drupal'); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Bootstrap Drupal. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // Proceed with PHPUnit tests as ...


10

My guess is that you're not including the phpunit.xml.dist file, which sits in the core folder. Either run tests from there (./vendor/bin/phpunit ../modules/your_module) or specify that file with -c. You can also provide your own phpunit.xml.dist file (see the Payment module as an example); then you can run PHPUnit from your module folder.


8

In the ruby world, TDD is facilitated by tools built into the framework. Factory Girl, Mocha, rSpec and others allow developers to create easily and dynamically tests that address the test cases needed. I've been frustrated as well by the lack to TDD tools in Drupal. My biggest issue with them is the amount of time it takes to run a single test. ...


8

Unit tests must have the following namespace: Drupal\Tests\dev_module\Unit. For complicated reasons, they also don't show up in the right group but they are all in the PHPUnit group. To run your unit tests with phpunit, just pass the module folder, that also makes it 10x faster for finding the tests: ./vendor/bin/phpunit -c core modules/dev_module


8

For kernel tests you need to specify which configurations you want installed. For instance, you'll need something like this in your test's setUp() method: $this->installConfig(['my_custom_module']); That takes an array of modules, so you'll probably need to specify additional modules (like filter, node, and field potentially). This assumes your field ...


8

If your new service is sufficiently independent (it doesn't have any or many of it own dependencies), then just create it the setUp(). And then if you have dependencies, you set up mocks (also called doubles). An example of a this in core is TimeTest::setUp() does: protected function setUp() { parent::setUp(); $this->requestStack = $this->...


7

You have a flaw in your code, module_load_include works by including files relative to the module base dir, so you should do module_load_include('inc', 'password_policy', 'constraints /constraint_uppercase'); to include password_policy/constraints/constraint_uppercase.inc When you do module_load_include('inc', 'password_policy', 'constraint_uppercase'); ...


7

BrowserTestBase and KernelTestBase use phpunit's 'run in separate process' feature which generates a new temporary php file from a template and then executes that. The output is provided as serialized php object which is then read back into the main thread an unserialized to yield the results. So at present that makes using Xdebug fairly difficult. We ...


7

Following on Bedir's answer, here is an example. I have a test that is using the class DurationService. DurationService uses the StringTranslationTrait as follows: class DurationService implements DurationServiceInterface { use StringTranslationTrait; ... To test this, I am doing the following (note, using a class that extends UnitTestCase): $...


7

You need to mock the services you will be using in your test, so that they return values. You can do that as follows: public function testDefaultBehavior() { $some_resolver = $this->getMockBuilder('Drupal\namespace\for\custom_utils\someresolver') ->disableOriginalConstructor() ->getMock(); $config_factory = $this->getMockBuilder('...


6

Simpletest automatically captures sent mails during a test and allows you to fetch them using $this->drupalGetMails() in the test class. I did something like this recently in tests I wrote for the Simplenews project, to test subscription confirmation here.


6

Open up firefox or chrome, and inspect the form (using firebug or any equivalent). If you see any element with the class 'honeypot-*' and a label 'Leave this field blank'. Then its a honeypot field and its enabled. To check if it works. Make the element visible by changing display: none to display: block, enter a value into the input box and hit submit. The ...


6

I would recommend you to consider testing http://visualwebsiteoptimizer.com/ for your use case. There is also a module http://drupal.org/project/visual_website_optimizer which makes the integration easier for you. From the FAQ page of VWO you can see that Uses cookies to track visitors Visitors see same variation even on repeat visits So that should solve ...


6

There are a few options for A/B testing a Drupal website, and all options will run the simple test requirements of this question (splitting traffic with specified percentages and keeping visitors in their initial experiences). The two basic approaches are using a paid external service to run the A/B tests or using a fully Drupal-integrated module. Here are ...


6

From the module.info file docs: ( quite long, but informative ) dependencies An array of other modules that your module requires. If these modules are not present, your module cannot be enabled. If these modules are present but not enabled, the administrator will be prompted with a list of additional modules to enable and may choose to enable the required ...


6

Consider using "php scripts/run-tests.sh" instead of "drush run-test". The former supports parallel test execution which improves runtime (as long as you have multiple test-classes). Compare: $ php scripts/run-tests.sh --url http://d45.l User ... Test run duration: 11 min 45 sec $ php scripts/run-tests.sh --url http://d45.l --concurrency 3 User ... Test run ...


6

See https://www.drupal.org/phpunit for better documentation. There are 4 test base classes: UnitTestBase, pure unit tests where everything but your code has to be mocked and only supports classes KernelTestBase, which provide a basic in-memory drupal environment. Much faster than browser tests but more tedious to set up, you have to explicitly install ...


6

StringTranslationTrait also has a setStringTranslation() method so you can inject a string translation mock for exactly that purpose.


6

Recent versions of PHPUnit swallow output. To get around this, you need to run with the --debug flag, eg $ ../vendor/bin/phpunit --debug modules/migrate/tests/src/Unit/process/FormatDateTest.php Then you can see printed output. Also note that PHPUnit will skip tests with output in them with the default phpunit.xml file, because it will deem them risky. ...


5

Each assert()ion you have in your test code will result in a pass, fail or even an exception. The uncaught exceptions are bad fails. If you're running your tests with scripts/run-tests.sh, then use the --verbose flag and you'll be able to see the result of each assertion and any associated error/s.


5

You could try the Masquerade module for Drupal. It allows you to switch users if you have the right permissions, so you could switch mid-session to a user with a different role etc. Another thing you could try, though non-Drupal, is multiple sessions within Firefox. Look here, here and here to see if that could help you.


5

A solution that I would very much like to test, but haven't so for, is combining a branching model like this one, not necessarily with git, with selenium automated testing. Every night, a tool like Jenkins pulls the development branch, runs drush make, drush site-install, and runs your selenium test suite. I also remember reading an empirical study about ...


5

This behavior is correct. SimpleTest sets up a new site environment for every test case, using a table name prefix. You probably forgot to install your module by overriding the setUp() method. Once your module is installed the query should work.


5

I don't use drush test-run, but Drush options are always in the form --xml=/var/tmp. c.f. http://drupal.org/node/1416198 From drush help test-run --xml Output verbose test results to a specified directory using the JUnit test reporting format. Useful for integrating with Jenkins. Try: sudo -u apache ...


5

It would seem to me that you'd want to leave Drupal doing its business logic thing accurately as a production site. Sending Mail (or "logging email") is the mail server's job ... so we just tell the server to not really send the emails. You can make this a development server configuration value using php.ini sendmail_path value. From this Stackoverflow post ...


5

The issue is definitely related to cache backends. Disable cache backends if you are testing using Simpletest classes.


Only top voted, non community-wiki answers of a minimum length are eligible