After building a custom install profile (example), I attempted to create a test for the profile, similar to StandardTest.
The test failed due to Drupal's inability to write the file to the desired folder in drupal_generate_test_ua() (sample output below). I've been unable to determine why this predictably happens with my install profile, but not with the Minimal profile (for example).
There is nothing that leads me to believe this has to do with environmental factors, but colleagues have had trouble reproducing, so I'm providing as much detail as possible here.
Environment details
- OSX 10.9.2
- Clean install of latest Acquia DevDesktop 2 Beta (2014-04-10).
- Drush 7.x
Steps to reproduce
1. Clone Drupal 8.x: git clone --branch 8.x http://git.drupal.org/project/drupal.git
2. Clone the test profile: In the /profiles folder, git clone --branch spelunky-profile git@github.com:jrbeeman/spelunky.git
3. Setup this code base as a site in DevDesktop: See Getting started and some Drupal 8-related notes.
4. Install the site, enable Testing, and run the simple install profile test:
drush si -v spelunky;
drush en simpletest;
drush test-run -v "Drupal\spelunky\Tests\SpelunkyTest";
You should be presented with a list of errors, all resulting from this first one:
Test drupal_generate_test_ua() failed: file_put_contents(/Users/jbeeman/Sites/spelunky/sites/simpletest/271652/.htkey): failed to open stream: Permission [error]
denied<pre class="backtrace">file_put_contents('/Users/jbeeman/Sites/spelunky/sites/simpletest/271652/.htkey',
'd3otdkJNnBNi3wRj9FKKdEk1lPFEym6jyWs2_SCM4hMsOrWmpxe-IKjDlRAO8uzXVYgzavWORQ')
drupal_generate_test_ua('simpletest271652')
Comment | File | Size | Author |
---|---|---|---|
#34 | 2246725-34.patch | 1.21 KB | lpeabody |
Issue fork drupal-2246725
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
jrbeemanThe attached patch modifies drupal_generate_test_ua() to add a call to file_prepare_directory(), which ensures that the destination folder for the htkey file exists before it attempts to write the file.
Comment #2
jrbeemanComment #3
jrbeemanUpdated issue description with environmental details and steps to reproduce.
Comment #4
jrbeemanAdded link to related issue #2171683: Remove all Simpletest overrides and rely on native multi-site functionality instead.
Comment #5
sunfile_prepare_directory()
cannot be used, because it requires a fully booted Drupal environment.The comment in the line directly above clearly states that Settings and settings.php is not available yet.
Curious, could you repeat + redo your steps to reproduce, but remove Drush entirely from the equation, please?
Unless your installation profile causes the regular Drupal installation procedure to prematurely end, Drush is really the only aspect here that could cause a misbehavior. There are many similar problems with Drush on D8, so that wouldn't be a surprise.
Comment #6
jrbeemanI re-ran the tests this morning using the UI (not Drush) and recreated the failure.
To help me better understand: Can you explain why you're concerned file_prepare_directory() won't be available in this case? Is file.inc not loaded, or are you indicating that it depends on some other method or variable that may not exist yet?
The key issue I'm encountering is that file_put_contents() fails because the directory into which it's attempting to put the file doesn't exist yet. What I'm struggling to see is how this works in any other instances, because file_put_contents() needs the directory to exist before it can put the file there.
Interestingly, the Standard profile tests still pass just fine. So, I wonder if the issue may be related to the content architecture created by the profile I've built. It contains several content types with fields of various types (file, entity reference, etc.). That's the only clear distinction I can note between my profile and the Standard profile.
Comment #7
TravisCarden CreditAttribution: TravisCarden commentedTo eliminate other variables, I was able to follow @jrbeeman's steps to reproduce on Ubuntu 14.04 with a standard, current AMP stack. So the problem seems dependent neither on any of the environment details listed in the summary nor on Drush.
Comment #8
clemens.tolboomfwiw in #2272879: Can not run a WebTestBase or KernelTest base tests from inside a simpletest the .htkey file is written without checking for the existing of the directory.
@jrbeeman what I think @sun is trying to say is calling file_prepare_directory() assumes a fully bootstrapped Drupal which is normally the case but simpletest runs its tests in a 'child site'.
You could try to test from the commandline like
Hope that helps?
Comment #9
jhedstromThis is at needs work.
Comment #10
akalata CreditAttribution: akalata commentedI'm also seeing this issue with a custom install profile. My error message is slightly different, but seems to be the same basic idea. I am able to run other tests locally (such as Drupal\standard\Tests\StandardTest) without issues.
System details:
OSX 10.10.3
Drupal 8.0.x-beta10
file_put_contents(/Users/Swan/Sites/locals/d8-profile.local/sites/simpletest/353164/.htkey): failed to open stream: Permission denied
Comment #11
akalata CreditAttribution: akalata commentedLooking into this a bit more. When I copy the "Standard" install profile, and rename everything to "Standard2", and run the test, and everything comes up green. So that's curious.
Comment #12
akalata CreditAttribution: akalata commentedWhat I've found from investigating my install profile is that adding the Update Manager module (update) to the dependencies causes the exception in the test (even when the test has nothing to do with updates).
Comment #14
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedFWIW I am also running into this issue on an install profile. The tests run and once the simpletest browser is clicking around the website, a http request on the target page/thread is made which triggers the exact same permission denied error.
Comment #15
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedPossible solution without bootstrapping?
Comment #16
benjy CreditAttribution: benjy at PreviousNext commentedPatch works as advertised, probably needs a test though.
Comment #18
kim.pepperRe-rolled for 8.2.x
Comment #19
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedThis is no longer an issue with BrowserTestBase as it sets the sites directory to be 777 after the installer completes. Test to demo the issue with the 777 commented out, but I think this can be closed.
Comment #20
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedComment #23
benjy CreditAttribution: benjy commentedThis issue still crops up using #2793445: Allow BTB to test an existing, already installed Drupal site instead of installing from scratch but you won't notice it unless you're sending PHP errors from the requests back to the tests :)
I actually had to make this mkdir($key_file_directory, 0775);
Comment #24
benjy CreditAttribution: benjy commentedA proper fix should probably be solved in the BTB issue but here it is for now anyway.
Comment #27
jibranThe problem is when the tests are run on already installed site the TestSitePath doesn't exist so this can cause an issue. See #2793445-86: Allow BTB to test an existing, already installed Drupal site instead of installing from scratch or https://gitlab.com/weitzman/drupal-test-traits/merge_requests/36/diffs#3... so this is not a bug but a task.
Comment #28
alexpottHmmm.... so the use case is to run tests against an existing database. We're not installing a site all right? So the files directory that's being used is the same as the site too?
I'm worried because in \Drupal\Core\DrupalKernel::findSitePath() we use
to get the site path. Even if there should be a separate test path I'm not sure here is the right place to create it. Atm the directory is created in \Drupal\Core\Test\FunctionalTestSetupTrait::prepareEnvironment() and I'm not sure we should change that.
Comment #29
jibranYes, that is right.
This is a really good question. If we go by this logic then we should need to update
\Drupal\Core\Test\TestDatabase
to accommodate that case.I think if we accommodate the case that testing DB is default DB in
\Drupal\Core\Test\TestDatabase
we can address that.I'm +1 to make
\Drupal\Core\Test\TestDatabase
more usable when site is already installed. What do you think?Comment #30
alexpottI don't think we should change
\Drupal\Core\Test\TestDatabase
at all. But maybe we should make it easy to override.Comment #31
jibranYeah, that makes sense.
Comment #32
jibranAny ideas how to proceed?
Comment #33
alexpottIt is tricky because the class is used both by the test runner and the site under test so it has to be overridable in both places. This is made even harder by the fact that in the site under test it is used before settings.php is loaded so overriding it there is not an option either. So we're going to need to be wrap drupal_valid_test_ua() to return one of these objects and to communicate the class to use in the SIMPLTEST_USER_AGENT.
Comment #34
lpeabody CreditAttribution: lpeabody commentedRe-rolled to apply to 8.6.x.
Comment #35
jibranIn Drupal testing trait library, we have an open merge request https://gitlab.com/weitzman/drupal-test-traits/merge_requests/62 to fix this issue. Please try it or similar approach on your projects.
Comment #43
alexverb CreditAttribution: alexverb as a volunteer commentedFor me the patch was not sufficient because it was not able to create the sites/simpletest folder. So for that I created a merge request that creates the directory recursively. Not sure why the tests are failing though...
Comment #47
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
MR should be updated for 10.1
Also IS should be updated as it references acquia dev desktop which support stopped in June 2021.
Comment #49
dpagini CreditAttribution: dpagini as a volunteer commentedI've actually recently run into this issue, and I have some more details that affected me here I wanted to share...
The fix here still solves my problem, and I can explain why as well.
So I ran into this issue with the following scenario...
* I am installing a site that uses a custom cron job to make a call out to an RSS feed, download the file, and store it locally for processing by the feeds module.
* Drupal core runs through the INSTALL steps... but the
install_configure_form
step actually hardens the simpletest site directory, and sets the folder to 555 (not writeable).* Drupal core at the end of the installation then triggers a cron. My job calls out to the internet. Drupal core has a custom testing middleware that intercepts that calls, and tries to add testing headers to the call, and ultimately calls
drupal_generate_test_ua()
, which tries to write the .htkey file to a hardened sites directory, and that fails... so my first cron run fails silently.I actually came across this b/c by enabling the "locale" module, the call to drupal_generate_test_ua() was made earlier in the stack, before the hardening, and the .htkey file gets placed successfully, and there is no more failure.