Files
sm-sitemap-plugin/filter_fixture_test.go
Jakub Zych ded1095c39 feat: hreflang alternates and the sitemap filter contract
- every listed address of a record with more than one listed locale
  carries xhtml:link alternates for each locale plus x-default on the
  default locale (D-55 to D-57); no alternate for a missing translation
- each page of records and the additional addresses go to the published
  contract.Filter in one batch: Exclude leaves the files with its reason,
  Group moves the address, LastMod seeds a new row (D-95, D-24)
- classes.Listing implements contract.Listing and is published in Register
2026-11-04 11:20:38 +01:00

60 lines
1.7 KiB
Go

package sitemap_test
import (
"context"
"strings"
"sync"
"git.golem15.com/golem15/sm-sitemap-plugin/contract"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/party"
)
// filterPluginID is a fake plugin that publishes a contract.Filter, as the
// SEO plugin does: it imports only the contract package.
const filterPluginID = "acme.filter"
// fakeFilter excludes addresses containing "noindex" (reason noindex) and
// puts addresses containing "promo" into the group offers. It records the
// batches it was asked.
type fakeFilter struct {
mu sync.Mutex
batches [][]contract.Candidate
}
func (f *fakeFilter) Decide(_ context.Context, batch []contract.Candidate) ([]contract.Decision, error) {
f.mu.Lock()
f.batches = append(f.batches, append([]contract.Candidate(nil), batch...))
f.mu.Unlock()
out := make([]contract.Decision, len(batch))
for i, c := range batch {
switch {
case strings.Contains(c.URL, "noindex"):
out[i] = contract.Decision{Exclude: true, Reason: contract.ReasonNoindex}
case strings.Contains(c.URL, "promo"):
out[i] = contract.Decision{Group: "offers"}
}
}
return out, nil
}
func (f *fakeFilter) Groups(context.Context) ([]contract.Group, error) {
return []contract.Group{{Key: "offers", Titles: map[string]string{"en": "Offers", "pl": "Oferty"}}}, nil
}
var theFilter = &fakeFilter{}
// filterPlugin is acme.filter.
type filterPlugin struct{}
func (filterPlugin) ID() string { return filterPluginID }
func (filterPlugin) Requires() []string { return nil }
func (filterPlugin) Register(app *backpack.App) error {
return app.Publish[contract.Filter](theFilter)
}
func (filterPlugin) Boot(*backpack.App) error { return nil }
func init() {
party.Register(filterPlugin{})
}