| 1 | # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| 2 | # contributor license agreements. See the NOTICE file distributed with | ||
| 3 | # this work for additional information regarding copyright ownership. | ||
| 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| 5 | # (the "License"); you may not use this file except in compliance with | ||
| 6 | # the License. You may obtain a copy of the License at | ||
| 7 | # | ||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | # | ||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | # See the License for the specific language governing permissions and | ||
| 14 | # limitations under the License. | ||
| 15 | # | ||
| 16 | package Apache::TestConfigPHP; | ||
| 17 | |||
| 18 | #things specific to php | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings FATAL => 'all'; | ||
| 22 | use File::Spec::Functions qw(catfile splitdir abs2rel); | ||
| 23 | use File::Find qw(finddepth); | ||
| 24 | use Apache::TestTrace; | ||
| 25 | use Apache::TestRequest; | ||
| 26 | use Apache::TestConfig; | ||
| 27 | use Apache::TestConfigPerl; | ||
| 28 | use Config; | ||
| 29 | |||
| 30 | @Apache::TestConfigPHP::ISA = qw(Apache::TestConfig); | ||
| 31 | |||
| 32 | my ($php_ini, $test_more); | ||
| 33 | |||
| 34 | { | ||
| 35 | # __DATA__ contains both php.ini and test-more.php | ||
| 36 | |||
| 37 | local $/ = "END_OF_FILE\n"; | ||
| 38 | |||
| 39 | $php_ini = <DATA>; | ||
| 40 | chomp $php_ini; | ||
| 41 | |||
| 42 | $test_more = <DATA>; | ||
| 43 | chomp $test_more; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | return shift->SUPER::new(@_); | ||
| 48 | } | ||
| 49 | |||
| 50 | my %warn_style = ( | ||
| 51 | html => sub { "<!-- @_ -->" }, | ||
| 52 | c => sub { "/* @_ */" }, | ||
| 53 | ini => sub { join '', grep {s/^/; /gm} @_ }, | ||
| 54 | php => sub { join '', "<?php\n", grep {s/^/# /gm} @_ }, | ||
| 55 | default => sub { join '', grep {s/^/\# /gm} @_ }, | ||
| 56 | ); | ||
| 57 | |||
| 58 | my %file_ext = ( | ||
| 59 | map({$_ => 'html'} qw(htm html)), | ||
| 60 | map({$_ => 'c' } qw(c h)), | ||
| 61 | map({$_ => 'ini' } qw(ini)), | ||
| 62 | map({$_ => 'php' } qw(php)), | ||
| 63 | ); | ||
| 64 | |||
| 65 | sub warn_style_sub_ref { | ||
| 66 | my ($self, $filename) = @_; | ||
| 67 | my $ext = $self->filename_ext($filename); | ||
| 68 | return $warn_style{ $file_ext{$ext} || 'default' }; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub configure_php_tests_pick { | ||
| 72 | my($self, $entries) = @_; | ||
| 73 | |||
| 74 | for my $subdir (qw(Response)) { | ||
| 75 | my $dir = catfile $self->{vars}->{t_dir}, lc $subdir; | ||
| 76 | next unless -d $dir; | ||
| 77 | |||
| 78 | finddepth(sub { | ||
| 79 | return unless /\.php$/; | ||
| 80 | |||
| 81 | my $file = catfile $File::Find::dir, $_; | ||
| 82 | my $module = abs2rel $file, $dir; | ||
| 83 | my $status = $self->run_apache_test_config_scan($file); | ||
| 84 | push @$entries, [$file, $module, $subdir, $status]; | ||
| 85 | }, $dir); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub write_php_test { | ||
| 90 | my($self, $location, $test) = @_; | ||
| 91 | |||
| 92 | (my $path = $location) =~ s/test//i; | ||
| 93 | (my $file = $test) =~ s/php$/t/i; | ||
| 94 | |||
| 95 | my $dir = catfile $self->{vars}->{t_dir}, lc $path; | ||
| 96 | my $t = catfile $dir, $file; | ||
| 97 | my $php_t = catfile $dir, $test; | ||
| 98 | return if -e $t; | ||
| 99 | |||
| 100 | # don't write out foo.t if foo.php already exists | ||
| 101 | return if -e $php_t; | ||
| 102 | |||
| 103 | $self->gendir($dir); | ||
| 104 | my $fh = $self->genfile($t); | ||
| 105 | |||
| 106 | print $fh <<EOF; | ||
| 107 | use Apache::TestRequest 'GET_BODY_ASSERT'; | ||
| 108 | print GET_BODY_ASSERT "/$location/$test"; | ||
| 109 | EOF | ||
| 110 | |||
| 111 | close $fh or die "close $t: $!"; | ||
| 112 | |||
| 113 | # write out an all.t file for the directory | ||
| 114 | # that will skip running all PHP test unless have_php | ||
| 115 | |||
| 116 | my $all = catfile $dir, 'all.t'; | ||
| 117 | |||
| 118 | unless (-e $all) { | ||
| 119 | my $fh = $self->genfile($all); | ||
| 120 | |||
| 121 | print $fh <<EOF; | ||
| 122 | use strict; | ||
| 123 | use warnings FATAL => 'all'; | ||
| 124 | |||
| 125 | use Apache::Test; | ||
| 126 | |||
| 127 | # skip all tests in this directory unless a php module is enabled | ||
| 128 | plan tests => 1, need_php; | ||
| 129 | |||
| 130 | ok 1; | ||
| 131 | EOF | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sub configure_php_inc { | ||
| 136 | my $self = shift; | ||
| 137 | |||
| 138 | my $serverroot = $self->{vars}->{serverroot}; | ||
| 139 | |||
| 140 | my $path = catfile $serverroot, 'conf'; | ||
| 141 | |||
| 142 | # make sure that require() or include() calls can find | ||
| 143 | # the generated test-more.php without using absolute paths | ||
| 144 | my $cfg = { php_value => "include_path $path", }; | ||
| 145 | $self->postamble(IfModule => $self->{vars}->{php_module}, $cfg); | ||
| 146 | |||
| 147 | # give test-more.php access to the ServerRoot directive | ||
| 148 | $self->postamble("SetEnv SERVER_ROOT $serverroot\n"); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub configure_php_functions { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | my $dir = catfile $self->{vars}->{serverroot}, 'conf'; | ||
| 155 | my $file = catfile $dir, 'test-more.php'; | ||
| 156 | |||
| 157 | $self->gendir($dir); | ||
| 158 | my $fh = $self->genfile($file); | ||
| 159 | |||
| 160 | print $fh $test_more; | ||
| 161 | |||
| 162 | close $fh or die "close $file: $!"; | ||
| 163 | |||
| 164 | $self->clean_add_file($file); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub configure_php_ini { | ||
| 168 | my $self = shift; | ||
| 169 | |||
| 170 | my $dir = catfile $self->{vars}->{serverroot}, 'conf'; | ||
| 171 | my $file = catfile $dir, 'php.ini'; | ||
| 172 | |||
| 173 | return if -e $file; | ||
| 174 | |||
| 175 | my $log = catfile $self->{vars}->{t_logs}, 'error_log'; | ||
| 176 | |||
| 177 | $self->gendir($dir); | ||
| 178 | my $fh = $self->genfile($file); | ||
| 179 | |||
| 180 | $php_ini =~ s/\@error_log\@/error_log $log/; | ||
| 181 | print $fh $php_ini; | ||
| 182 | |||
| 183 | close $fh or die "close $file: $!"; | ||
| 184 | |||
| 185 | $self->clean_add_file($file); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub configure_php_tests { | ||
| 189 | my $self = shift; | ||
| 190 | |||
| 191 | my @entries = (); | ||
| 192 | $self->configure_php_tests_pick(\@entries); | ||
| 193 | $self->configure_pm_tests_sort(\@entries); | ||
| 194 | |||
| 195 | my %seen = (); | ||
| 196 | |||
| 197 | for my $entry (@entries) { | ||
| 198 | my ($file, $module, $subdir, $status) = @$entry; | ||
| 199 | |||
| 200 | my @args = (); | ||
| 201 | |||
| 202 | my $directives = $self->add_module_config($file, \@args); | ||
| 203 | |||
| 204 | my @parts = splitdir $file; | ||
| 205 | my $test = pop @parts; | ||
| 206 | my $location = $parts[-1]; | ||
| 207 | |||
| 208 | debug "configuring PHP test file $file"; | ||
| 209 | |||
| 210 | if ($directives->{noautoconfig}) { | ||
| 211 | $self->postamble(""); # which adds "\n" | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | unless ($seen{$location}++) { | ||
| 215 | $self->postamble(Alias => [ catfile('', $parts[-1]), catfile(@parts) ]); | ||
| 216 | |||
| 217 | my @args = (AddType => 'application/x-httpd-php .php'); | ||
| 218 | |||
| 219 | $self->postamble(Location => "/$location", \@args); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | $self->write_php_test($location, $test); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | 1; | ||
| 228 | |||
| 229 | __DATA__ |