Skip to content

Commit 3953592

Browse files
authored
Merge pull request #68 from gtt-project/fix/invalid_multi_polygon_for_project
Show error message when updating project geometry with invalid multi polygon
2 parents 855c50f + e10b306 commit 3953592

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ After restarting Redmine, you should be able to see the Redmine GTT plugin in th
5050

5151
More information on installing (and uninstalling) Redmine plugins can be found here: http://www.redmine.org/wiki/redmine/Plugins
5252

53+
## How to run test
54+
55+
After the installation, you can run the plugin test by the following command:
56+
57+
```
58+
RAILS_ENV=test NAME=redmine_gtt bundle exec rake redmine:plugins:test
59+
```
60+
5361
## How to use
5462

5563
1. Go to plugin configuration for global settings

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# English strings go here for Rails i18n
22
en:
33
error_invalid_json: must be valid JSON
4+
error_unable_to_update_project_gtt_settings: "Unable to update project GTT settings (%{value})"
45

56
field_default: Default for new projects
67
field_geometry: "Geometry"

config/locales/ja.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Japanese strings go here for Rails i18n
22
ja:
33
error_invalid_json: "正しいJSONにしてください"
4+
error_unable_to_update_project_gtt_settings: "プロジェクトのGTTの設定を更新できません (%{value})"
45

56
field_default: "新規プロジェクトのデフォルト"
67
field_geometry: "所在地"

lib/redmine_gtt/actions/update_project_settings.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ def update_project
2626
if tile_source_ids = @form.gtt_tile_source_ids
2727
@project.gtt_tile_source_ids = tile_source_ids
2828
end
29-
@project.geojson = @form.geojson
29+
30+
begin
31+
@project.geojson = @form.geojson
32+
rescue RGeo::Error::InvalidGeometry => e
33+
@project.errors.add(:geom, :invalid)
34+
return false
35+
end
3036

3137
@project.save
3238
end

lib/redmine_gtt/patches/projects_controller_patch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def update_gtt_configuration
4747
if r.settings_saved?
4848
flash.now[:notice] = l(:notice_successful_update)
4949
else
50-
flash.now[:error] = r.error
50+
flash.now[:error] = l(:error_unable_to_update_project_gtt_settings, "#{r.error}")
5151
end
5252

5353
end

test/test_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def point_geojson(coordinates)
1414
{'type'=>'Feature','geometry'=>{ 'type'=>'Point','coordinates'=> coordinates}}.to_json
1515
end
1616

17+
def multipolygon_geojson(coordinates)
18+
{'type'=>'Feature','geometry'=>{ 'type'=>'MultiPolygon','coordinates'=> coordinates}}.to_json
19+
end
20+
1721
def test_geom
1822
RedmineGtt::Conversions::WkbToGeom.("01030000000100000006000000C84B374110E76040381DD011545A4140C84B3739ACE96040F07E6DCC7A594140C84B37F199E960403CBC2D58E2554140C84B373917E8604098CBC3E188564140C84B37FD36E66040F24C2E959D564140C84B374110E76040381DD011545A4140")
1923
end

test/unit/update_project_settings_test.rb

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require_relative '../test_helper'
22

3-
class UpdateProjectSettingsTest < ActiveSupport::TestCase
3+
class UpdateProjectSettingsTest < GttTest
44
fixtures :projects
55

66
test 'should save tile sources' do
@@ -15,6 +15,49 @@ class UpdateProjectSettingsTest < ActiveSupport::TestCase
1515
p.reload
1616
assert_equal [ts], p.gtt_tile_sources.to_a
1717
end
18+
19+
test 'should validate invalid multipolygon geometry' do
20+
p = Project.find 'ecookbook'
21+
coordinates = [
22+
[
23+
[[135.0, 35.0], [136.0, 35.0], [136.0, 36.0], [135.0, 36.0], [135.0, 35.0]]
24+
],
25+
[
26+
[[136.0, 35.0], [137.0, 35.0], [137.0, 36.0], [136.0, 36.0], [136.0, 35.0]]
27+
]
28+
]
29+
30+
form = GttConfiguration.from_params geojson: multipolygon_geojson(coordinates)
31+
form.project = p
32+
r = RedmineGtt::Actions::UpdateProjectSettings.( form )
33+
34+
assert_not r.settings_saved?
35+
36+
p.reload
37+
assert_include 'Geometry is invalid', p.errors.full_messages
38+
end
39+
40+
test 'should save valid multipolygon geometry' do
41+
p = Project.find 'ecookbook'
42+
coordinates = [
43+
[
44+
[[135.0, 35.0], [136.0, 35.0], [136.0, 36.0], [135.0, 36.0], [135.0, 35.0]]
45+
],
46+
[
47+
[[137.0, 35.0], [138.0, 35.0], [138.0, 36.0], [137.0, 36.0], [137.0, 35.0]]
48+
]
49+
]
50+
51+
form = GttConfiguration.from_params geojson: multipolygon_geojson(coordinates)
52+
form.project = p
53+
r = RedmineGtt::Actions::UpdateProjectSettings.( form )
54+
55+
assert r.settings_saved?
56+
57+
p.reload
58+
assert_equal coordinates, JSON.parse(p.geojson)['geometry']['coordinates']
59+
end
60+
1861
end
1962

2063

0 commit comments

Comments
 (0)