Questions tagged [testing]

Use it for questions about automated Drupal tests.

Filter by
Sorted by
Tagged with
3 votes
1 answer
2k views

Multi-user/role testing, best practices

Am loving using Drupal 6 for creating a site with more than one user "type", with different Content Profiles, different Roles with associated Permissions, different access to nodes etc. My current ...
Craig Moore's user avatar
2 votes
1 answer
224 views

Mock a service while running a BrowserTest?

I am trying to test the output of a controller that returns some information in a given page. This controller has a custom http client injected into it. I want to create a BrowserTest that can ...
Kevin's user avatar
  • 27.8k
1 vote
2 answers
772 views

Simpletest fails with "invalid database prefix" - no other information given

I am trying to setup a simple test that tests some breadcrumb altering code I have introduced only for a specific content type. Here is the test, still just barebones: namespace Drupal\...
Kevin's user avatar
  • 27.8k
23 votes
3 answers
5k views

How do I write automated tests?

I saw that Drupal 7 includes some kind of testing framework (I haven't tried it out yet since my existing sites are still in Drupal 6 and likely won't migrate any time soon). How can you write ...
Chaulky's user avatar
  • 6,453
12 votes
4 answers
5k views

SymfonyTestsListener does not exist on PHP Unit Test

When I try to run a unit test I get an error that SymfonyTestsListener does not exist. Am I running this correctly? ../../vendor/bin/phpunit -c phpunit.xml ../modules/custom/my_module/ Class "...
albertski's user avatar
  • 3,447
10 votes
1 answer
10k views

"\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container."

I am attempting to write a PHPUnit test for a service of mine. My test looks like the following. class CustomServiceTest extends UnitTestCase { public function testDefaultBehavior() { $...
Potney Switters's user avatar
8 votes
1 answer
3k views

how to unit test an http client service with DI in drupal 8?

I have a service the consumes data from an external source. I would like to unit test the service and I am not sure how to get the test set up. To start with I would like to just test 1 method. Here ...
awm's user avatar
  • 2,459
6 votes
5 answers
2k views

How to create a unit test for a class which uses a trait?

Simple as this: class A { use StringTranslationTrait; public function something() { $b = 1+2; $this->t($b); return $b; } } I would like to unit test the something() method, ...
ssibal's user avatar
  • 3,165
6 votes
3 answers
821 views

Redirect SimpleTest queries to a different database

How can I tell SimpleTest to use a different database than the default connection? I want to be able to run the tests against an in memory database while keeping the local data on disk. UPDATE: So ...
gregghz's user avatar
  • 767
5 votes
1 answer
787 views

How can I debug Simpletests for a custom module?

I am struggling to debug PHPUnit Tests (with xdebug) in a custom D8 Module from PHPStorm or via the command line. Is it possible to do this? I'm running an Acquia Bolt generated site on a Drupal-VM ...
Justin Levi Winter's user avatar
5 votes
1 answer
11k views

Writing unit tests for a method that calls static methods from another class

I have a custom service defined by my module and in one of its methods I call a static method from a class defined by a module mine depends on. Since this method is static I do not need to instantiate ...
bleen's user avatar
  • 1,500
5 votes
4 answers
2k views

How to test a code where the config is involved as parameter?

I'm new to OOP concepts, also PHPUnit Testing is new for me. I want to get the configuration in my testcase. XYZFormResult.php namespace Drupal\xyz\XYZFormResult; use Drupal\Core\Config\...
Siva's user avatar
  • 103
4 votes
1 answer
1k views

Behat test fails, unable to find Username field using @javascript

My Behat test fails as soon as I add @javascript Error: Form field with id|name|label|value|placeholder "Username" not found. When I remove @javascript, test is passed without any error. Given I ...
meez's user avatar
  • 1,333
4 votes
1 answer
290 views

Is it possible to write test against the current database?

I'm building some modules onto an existing and somewhat active site. From my understanding, SimpleTest creates a new, temporary database and effectively reinstalls a copy of Drupal. However, I'd ...
Rick's user avatar
  • 2,974
3 votes
1 answer
1k views

How can I mock a class during a functional request that is called from within a controller?

I am working on a custom module that from a controller calls Elastic search and returns a search result. I am trying to write a simple Functional Php Unit test just to make sure the results are ...
albertski's user avatar
  • 3,447
3 votes
1 answer
343 views

How to inspect outgoing email content during a SimpleTest?

I see that SimpleTest has a few built in mail functions to check for send success etc, but how can I send a message, assert its content and then click a link within that content during a test? What ...
Citricguy's user avatar
  • 7,924
3 votes
2 answers
4k views

Call to undefined function t() in an Unit test

I'm trying to test a method from the dev version of the Allow a content type only once (Only One) module that have a t() call inside: <?php namespace Drupal\onlyone\Utility; class ...
Adrian Cid Almaguer's user avatar
2 votes
1 answer
923 views

How to test an alter hook in your .module with ModuleHandler service?

I have setup a kernel test to test a function in my module: /** * Implements hook_link_alter(). */ function mymodule_link_alter(&$variables) { // ... code } In my test, I tried: $variables =...
Kevin's user avatar
  • 27.8k
2 votes
1 answer
404 views

How do I test a CKEditor 4 text format field with Behat/Mink?

I am using an @javascript test in Behat on CircleCI to check my Drupal 8 site. When I use the standard I fill in "my field" with "value" step, it works for fields with the plain ...
Patrick Kenny's user avatar
2 votes
2 answers
639 views

using DrupalWebTestCase - any trick to prevent it from running from scratch each time?

It makes sense to run each test in a completely clean environment. However, before tests can be run, they have to be developed, during this process it is so frustrating to have to wait 3 or 4 minutes ...
camcam's user avatar
  • 2,111
2 votes
1 answer
873 views

Why am i getting PDO exception while running the following test from console?

I am writing a module to return common parent of 2 revision in a linear entity. For example : After editing a node 5 times, we get a graph similar to 1->2->3->4->5 where 1 is the actual ...
Rakesh Verma's user avatar
2 votes
1 answer
466 views

How to write phpUnit tests for hook_form_alter?

I have 1 function that is returning cancel form button. This function is in my .module file. function _cancel_button(array &$form, FormStateInterface &$form_state) { // Adding buttons to ...
Sugandh Khanna's user avatar
2 votes
2 answers
3k views

Creating webform submissions programatically in order to use with SimpleTest

I'm trying to build out a SimpleTest to test out functionality on submitted webforms. I need to simulate loading a drupal webform node, applying fake data, and submitting the form (triggering a ...
Rick's user avatar
  • 2,974
2 votes
0 answers
157 views

sites/simpletest permissions are set to 777, but still "permission denied" error

I am running a WebTestBase test and getting the following warnings: But the files and folders within sites/simpletest are set to 777. Drupal does already write within this folder during the test ...
jepster's user avatar
  • 953
1 vote
1 answer
1k views

"Failed to set field" Problem with SimpleTest, testing a form

With SimpleTests, I want to test a form similar to : // Textfield : $form['fieldset_name']['test'] = array( '#type' => 'textfield', ... ); // Submit : $form['fieldset_name']['...
B2F's user avatar
  • 937
1 vote
0 answers
25 views

Is there a way to test or spoof different user access? [duplicate]

Possible Duplicate: Multi-user/role testing, best practices My current style in testing different user access is opening a different browser or opening a private session and logging into a ...
radj's user avatar
  • 1,026
0 votes
2 answers
562 views

How do I debug a the output errors on SimpleTest when I'm not sure where in the script the error occurred?

I'm trying to build a simple test. I'm having trouble submitting webforms. With help form a previous question I've cobbled together these functions within the larger class. They completely work in ...
Rick's user avatar
  • 2,974
0 votes
1 answer
264 views

Simpletest: database exception during setUp

I'm writing lots and lots of test cases for different modules and have come across a rather strange error about which I'm hoping others may have more insight. In my setUp function override, the first ...
meustrus's user avatar
  • 328
0 votes
1 answer
222 views

Simpletest for Sending Mail

I've written the following testcase to send an email. In test results output it says "1 e-mail was sent during this test." but acutally it's not. I tried another way, I put the function in menu ...
Krishna Mohan's user avatar
0 votes
1 answer
81 views

Easy database reconfiguration for migration [duplicate]

I have build a Drupal site on my local development server and I am preparing to move it to my test server and eventually my live server. I know where to find the database settings in my settings.php ...
Hoytman's user avatar
  • 713