]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_setup_website.py
Merge pull request #2542 from lonvia/update-phpunit
[nominatim.git] / test / python / tools / test_refresh_setup_website.py
1 """
2 Tests for setting up the website scripts.
3 """
4 import subprocess
5
6 import pytest
7
8 from nominatim.tools import refresh
9
10 @pytest.fixture
11 def test_script(tmp_path):
12     (tmp_path / 'php').mkdir()
13
14     website_dir = (tmp_path / 'php' / 'website')
15     website_dir.mkdir()
16
17     def _create_file(code):
18         outfile = website_dir / 'reverse-only-search.php'
19         outfile.write_text('<?php\n{}\n'.format(code), 'utf-8')
20
21     return _create_file
22
23
24 @pytest.fixture
25 def run_website_script(tmp_path, project_env, temp_db_conn):
26     project_env.lib_dir.php = tmp_path / 'php'
27
28     def _runner():
29         refresh.setup_website(tmp_path, project_env, temp_db_conn)
30
31         proc = subprocess.run(['/usr/bin/env', 'php', '-Cq',
32                                tmp_path / 'search.php'], check=False)
33
34         return proc.returncode
35
36     return _runner
37
38
39 def test_basedir_created(tmp_path, project_env, temp_db_conn):
40     webdir = tmp_path / 'website'
41
42     assert not webdir.exists()
43
44     refresh.setup_website(webdir, project_env, temp_db_conn)
45
46     assert webdir.exists()
47
48
49 @pytest.mark.parametrize("setting,retval", (('yes', 10), ('no', 20)))
50 def test_setup_website_check_bool(monkeypatch, test_script, run_website_script,
51                                   setting, retval):
52     monkeypatch.setenv('NOMINATIM_CORS_NOACCESSCONTROL', setting)
53
54     test_script('exit(CONST_NoAccessControl ? 10 : 20);')
55
56     assert run_website_script() == retval
57
58
59 @pytest.mark.parametrize("setting", (0, 10, 99067))
60 def test_setup_website_check_int(monkeypatch, test_script, run_website_script, setting):
61     monkeypatch.setenv('NOMINATIM_LOOKUP_MAX_COUNT', str(setting))
62
63     test_script('exit(CONST_Places_Max_ID_count == {} ? 10 : 20);'.format(setting))
64
65     assert run_website_script() == 10
66
67
68 def test_setup_website_check_empty_str(monkeypatch, test_script, run_website_script):
69     monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', '')
70
71     test_script('exit(CONST_Default_Language === false ? 10 : 20);')
72
73     assert run_website_script() == 10
74
75
76 def test_setup_website_check_str(monkeypatch, test_script, run_website_script):
77     monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'ffde 2')
78
79     test_script('exit(CONST_Default_Language === "ffde 2" ? 10 : 20);')
80
81     assert run_website_script() == 10
82
83
84 def test_relative_log_file(project_env, monkeypatch, test_script, run_website_script):
85     monkeypatch.setenv('NOMINATIM_LOG_FILE', 'access.log')
86
87     expected_file = str(project_env.project_dir / 'access.log')
88     test_script(f'exit(CONST_Log_File === "{expected_file}" ? 10 : 20);')
89
90     assert run_website_script() == 10
91