Allow the profile used for tests to be set via a variable

pwolanin - July 7, 2008 - 18:18
Project:Drupal
Version:7.x-dev
Component:simpletest.module
Category:feature request
Priority:normal
Assigned:pwolanin
Status:patch (code needs review)
Description

Per http://drupal.org/node/279455

Right now drupal_web_test_case always uses the default profile when installing. This is problematic for a couple reasons - for example, I can't test an alternate profile to make sure it runs correctly.

#1

pwolanin - July 7, 2008 - 18:29
Assigned to:Anonymous» pwolanin
Status:active» patch (code needs review)

Here's essentially the same patch for 7.x as #2 on that issue.

AttachmentSize
variable-profile-7x-279515-1.patch1.78 KB

#2

pwolanin - July 7, 2008 - 18:42

So, I think this would now allow you to test different profiles using code like:

<?php
class BasicInstallProfileTestCase extends DrupalWebTestCase {
  protected
$original_profile;

 
/**
   * Implementation of getInfo().
   */
 
function getInfo() {
    return array(
     
'name' => t('Basic Profile functionality'),
     
'description' => t('Test profiles'),
     
'group' => t('System')
    );
  }

 
/**
   * Implementation of setUp().
   */
 
function setUp() {
     
$this->original_profile = variable_get('web_test_case_profile', NULL);
     
variable_set('web_test_case_profile', 'basic');
     
parent::setUp();
  }

  function
tearDown() {
    
parent::tearDown();
     if (!empty(
$this->original_profile)) {
        
variable_set('web_test_case_profile', $this->original_profile);
     }
  }

 
/**
   * Test that profile features are enabled.
   */
 
function testBasicProfileFeatures() {

  }
}

class
BlogInstallProfileTestCase extends DrupalWebTestCase {
  protected
$original_profile;

 
/**
   * Implementation of getInfo().
   */
 
function getInfo() {
    return array(
     
'name' => t('Blog Profile functionality'),
     
'description' => t('Test profiles'),
     
'group' => t('System')
    );
  }

 
/**
   * Implementation of setUp().
   */
 
function setUp() {
     
$this->original_profile = variable_get('web_test_case_profile', NULL);
     
variable_set('web_test_case_profile', 'blog');
     
parent::setUp();
  }

  function
tearDown() {
    
parent::tearDown();
     if (!empty(
$this->original_profile)) {
        
variable_set('web_test_case_profile', $this->original_profile);
     }
  }

 
/**
   * Test that profile features are enabled.
   */
 
function testBlogProfileFeatures() {

  }
}
?>

#3

boombatower - July 7, 2008 - 20:45

I think it should be simpler and more documented.

Possible

<?php
$this
->setInstallProfile('web_test_case_profile');
$this->setUp();

....
?>

and on tearDown the class variable $install_profile could be reset.

#4

pwolanin - July 7, 2008 - 23:20

how about the attached?

One doesn't even need to reset the variable, I think, since this will be fresh for each class that's instantiated.

AttachmentSize
variable-profile-7x-279515-4.patch3.3 KB

#5

catch - July 7, 2008 - 23:27

I like the function name. Useful functionality to have. Not tested yet.

#6

boombatower - July 7, 2008 - 23:30
Status:patch (code needs review)» patch (code needs work)

I like this implementation much better although I think in needs to be reset in tearDown so that the next test will always use the default profile.

#7

pwolanin - July 8, 2008 - 00:48
Status:patch (code needs work)» patch (code needs review)

patch with bonus test.

I still don't think the addition to tearDown is really needed, but it doesn't hurt.

AttachmentSize
variable-profile-7x-279515-7a.patch5.27 KB

#8

Dries - July 8, 2008 - 00:58

Personally, I think setInstallProfile is behaving a little funny.

1. The return value can be 3 different things. Because this is a class, we don't necessarily have to do the funny return value thing for setInstallProfile.

2. To reset the install profile, why don't we set it to 'default' using $this->setInstallProfile('default') instead of $this->setInstallProfile(NULL, TRUE)? It seems like reset is not needed?

#9

pwolanin - July 8, 2008 - 01:41

right, we could also set a class variable rather than the internal static - cleaned up version attached.

AttachmentSize
variable-profile-7x-279515-9.patch5.94 KB

#10

cwgordon7 - July 11, 2008 - 03:00

Why the line:

"+ $this->test_profile = variable_get('web_test_case_profile', 'default');"

?

#11

pwolanin - July 11, 2008 - 14:45

Because those building a new or complicated profile might want to run ALL tests with that profile as the install and/or a packaged distribution of Drupal might not include the default profile.

#12

pwolanin - July 12, 2008 - 00:41

from IRC conversion, I misunderstood the cwgordon's concern - the code was unclear (and only worked by luck) - it's clearer and more reliable to move the determination of the profile to prior to where the DB prefix is altered.

AttachmentSize
variable-profile-7x-279515-12.patch6.27 KB

#13

dropcube - July 15, 2008 - 16:12

Currently, there are a lot of assertions relying on the HTML response. For example, drupalLogout checks if the user name field is found when requesting the /user page. If we are going to test alternate profiles that may install multiple modules we should have in mind that such modules may alter the output in any way. For example, consider a module that alters or replace the user login form, then every test that uses drupalLogout will fail due to the absence of the name field in which drupalLogout bases it's assertions.

So, here I see again the real need of having data available to make assertions, and not basing assertions 100% on the HTML response. See #266220: Exposing metadata to tests

#14

pwolanin - July 15, 2008 - 16:24

@dropcube - yes, that's certainly true - but I'm not sure there is any ready way around that. This patch just enables the use of other profiles - so if you do, obviously you'll have to take that into consideration when interpreting test results.

#15

Damien Tournoud - July 15, 2008 - 16:28
Title: Allow the profile used for tests to be set via a variable» Allow the profile used for tests to be set via a variable

@dropcube: I already expressed my concerns about #266220: Exposing metadata to tests. If I understand pwolanin approach here, we want to test profiles themselves (ie. have a default.test and a expert.test), not use existing tests on different profile configurations.

Again: we are testing Drupal in its standard configuration. We cannot even imagine testing every possible configuration out there.

#16

pwolanin - July 16, 2008 - 04:39

@Damien Tournoud - one motivation is to allow you to test profiles - but this patch enables a number of possibilities

#17

dropcube - July 16, 2008 - 18:22

The .test file attached uses a more generic approach by implementing a class InstallProfileTestCase which other profile tests may extend. This class contains common functions, for example, _testInstalledModules(). Also a test for the default profile has been added.

AttachmentSize
install.test2.52 KB

#18

dropcube - July 17, 2008 - 03:27

Here is the patch based on pwolanin's #12 with the approach described at #17

AttachmentSize
variable-profile-7x-279515-18.patch7.39 KB

#19

pwolanin - July 17, 2008 - 16:49
Status:patch (code needs review)» patch (code needs work)

If InstallProfileTestCase is supposed to be a generic base class to be extended, it should not have any tests in it.

#20

dropcube - July 17, 2008 - 17:26

Here is the updated patch. The base class does not contain test functions now, but assertions.

AttachmentSize
variable-profile-7x-279515-20.patch7.58 KB

#21

dropcube - July 17, 2008 - 17:27
Status:patch (code needs work)» patch (code needs review)

Install profiles Tests: 25 passes, 0 fails, 0 exceptions

#22

pwolanin - July 17, 2008 - 18:31

since drupal_verify_profile() does some extra checking and might eliminate some modules from the list in the profile, I'm not sure we want to use it for the test. I'd suggest reproducing the first part of the code from that function.

#23

dropcube - July 18, 2008 - 02:13

I am attaching an updated version of the file install.test for review and not a patch because seems to be a conflict in drupal_web_test_case.php due to #231190: Page Cache doesn't work with HEAD requests

In the test now are implemented #22 pwolanin's suggestions and added other tests for the default profile. Will submit a patch later.

AttachmentSize
install.test4.55 KB

#24

dropcube - July 19, 2008 - 15:42

Here is an updated patch against HEAD.

AttachmentSize
variable-profile-7x-279515-24.patch9.47 KB

#25

pwolanin - July 19, 2008 - 17:45

This looks pretty good - made a few tweaks. For example, not every test needs to check that the invalid profile name is rejected.

AttachmentSize
variable-profile-7x-279515-25.patch9.22 KB

#26

moshe weitzman - October 1, 2008 - 16:34

subscribe. a worthwhile patch. not time to test now though.

 
 

Drupal is a registered trademark of Dries Buytaert.